develooper Front page | perl.beginners | Postings from February 2002

Re: To create a hash of lists

Thread Previous
Jeff 'japhy' Pinyan
February 21, 2002 16:59
Re: To create a hash of lists
Message ID:
On Feb 21, said:

>Using the following code, I want to create a hash or individual lists.  Of
>course, this code doesn't work....
>What do I need to change to correct my error?

push() requires an array.  $foo{bar} is a scalar.  @{ $foo{bar} } is an

>push($printQueue{A}, 'This is the first line in A');
>push($printQueue{A}, 'This is the second line in A');
>push($printQueue{A}, 'This is the third line in A');
>push($printQueue{A}, 'This is the fourth line in A');
>push($printQueue{A}, 'This is the fifth line in A');

  push @{ $printQueue{A} }, ...;

You could just declare your hash as

  my %printQueue = (
    A => [ 'first', 'second', ... ],
    B => [ ... ],
    C => [ ... ],

>foreach my $key ('A', 'B', 'C') {
>	foreach my $text ($printQueue{$key}) {
>		print("$text \n");
>	}

Then, to loop over the array references, use

  foreach my $text (@{ $printQueue{$key} }) { ... }

Jeff "japhy" Pinyan
RPI Acacia brother #734
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About