in reply to Perl for loop
Hi MissPerl,
Your question is not very clear, but let's say that you mean you want to loop through two lists of things at the same time. You could store the things in two arrays and use the same index on each array as you go through the loop. That seems to be what you were trying to do.
... but you'd be better off storing your data in a hash, then you don't have to worry about keeping the index the same, or if the lists get out of order:my @names = ('Fred', 'Wilma', 'Barney', 'Betty'); my @ages = ( 40, 38, 42, 41 ); for my $i ( 0 .. 3 ) { print "$names[ $i ] is $ages[ $i ]\n"; }
my %flintstones = ( Fred => 40, Wilma => 38, Barney => 42, Betty => 41, ); for my $name ( keys %flintstones ) { print "$name is $flintstones{ $name }\n"; }
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl for loop
by MissPerl (Sexton) on Jun 02, 2017 at 07:15 UTC | |
by AnomalousMonk (Archbishop) on Jun 02, 2017 at 08:46 UTC |