use hash in a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • a

    use hash in a list

    my(%hashA,%hash B)
    how to populate %hashA and %hashB that are inside a list?
    Thanx


  • Jim Gibson

    #2
    Re: use hash in a list

    In article <lQSTe.145443$H k.7356@pd7tw1no >, <a@mail.com> wrote:
    [color=blue]
    > my(%hashA,%hash B)
    > how to populate %hashA and %hashB that are inside a list?
    > Thanx[/color]

    What does "hash inside a list" mean? I can guess that you are referring
    to this construct for one hash:

    my %hash = ( 'a' => 1, 'b' => 2 );

    extended to two hashes:

    my(%hash1,%hash 2) = ('a'=>1, 'b'=>2), ('c'=>3, 'd'=>4);

    except that doesn't work ("Useless use of a constant in void context").

    This compiles:

    my(%hash1,%hash 2) = (('a'=>1, 'b'=>2), ('c'=>3, 'd'=>4));

    except that all of the elements get assigned to %hash1. This is typical
    for Perl, in that an array involved in an assignment will tend to
    gobble up all remaining lvalues.

    The short answer to your question is then: "You don't".

    Is it so hard to write two lines?

    my %hash1 = ('a'=>1, 'b'=>2);
    my %hash2 = ('c'=>3, 'd'=>4);

    If this is not what you want, then please be more explicit. It always
    helps to include a short, complete program that demonstrates the
    problem you are having and explain why the program is not doing what
    you want.

    FYI: this newsgroup is defunct. Please use comp.lang.perl. misc in the
    future.

    Comment

    • a

      #3
      Re: use hash in a list

      I am going to write a program that is able to generate a list for employee
      information.
      The information is stored in a text file. The format is as follows:
      Peter NY English
      Jane ON French
      Charles NY English

      So, the program will ask the user to input the name of language, and output
      the list of the employee information.
      e.g:
      Input
      Please input language: English
      Output
      Peter NY
      Charles NY

      What data structure should I use?
      Thanx

      "Jim Gibson" <jgibson@mail.a rc.nasa.gov>
      ???????:0809200 50713025927%jgi bson@mail.arc.n asa.gov...[color=blue]
      > In article <lQSTe.145443$H k.7356@pd7tw1no >, <a@mail.com> wrote:
      >[color=green]
      > > my(%hashA,%hash B)
      > > how to populate %hashA and %hashB that are inside a list?
      > > Thanx[/color]
      >
      > What does "hash inside a list" mean? I can guess that you are referring
      > to this construct for one hash:
      >
      > my %hash = ( 'a' => 1, 'b' => 2 );
      >
      > extended to two hashes:
      >
      > my(%hash1,%hash 2) = ('a'=>1, 'b'=>2), ('c'=>3, 'd'=>4);
      >
      > except that doesn't work ("Useless use of a constant in void context").
      >
      > This compiles:
      >
      > my(%hash1,%hash 2) = (('a'=>1, 'b'=>2), ('c'=>3, 'd'=>4));
      >
      > except that all of the elements get assigned to %hash1. This is typical
      > for Perl, in that an array involved in an assignment will tend to
      > gobble up all remaining lvalues.
      >
      > The short answer to your question is then: "You don't".
      >
      > Is it so hard to write two lines?
      >
      > my %hash1 = ('a'=>1, 'b'=>2);
      > my %hash2 = ('c'=>3, 'd'=>4);
      >
      > If this is not what you want, then please be more explicit. It always
      > helps to include a short, complete program that demonstrates the
      > problem you are having and explain why the program is not doing what
      > you want.
      >
      > FYI: this newsgroup is defunct. Please use comp.lang.perl. misc in the
      > future.[/color]



      Comment

      • Joe Smith

        #4
        Re: use hash in a list

        a wrote:
        [color=blue]
        > Peter NY English
        > Jane ON French
        > Charles NY English[/color]

        my %employee_by_la nguage;
        while (<>) {
        my($name_co,$la ng) =~ /(.* \w\w) (.*)/;
        push @{$employee_by_ language{$lang} },$name_co;
        }

        Comment

        Working...