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

Re: To create a hash of lists

Thread Previous
From:
Jeff 'japhy' Pinyan
Date:
February 21, 2002 16:59
Subject:
Re: To create a hash of lists
Message ID:
Pine.GSO.4.21.0202211957020.19708-100000@crusoe.crusoe.net
On Feb 21, William.Crain@Hurlburt.af.mil 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
array.

>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      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** 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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About