Unless the array is huge, it's easier to access the values directly through the for loop
$hash{"array"} = ['a','b','c','d','e'];
print for @{ $hash{ array } };
a
b
c
d
e
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
| [reply] [d/l] |
$ perl -MBenchmark=timethese -e <<'EOC'
$a = [ 0 ..5_000 ];
timethese( 2_000,
{ index => sub { for( my $i = 0; $i <= $#{ $a }; $i++ ) { $b = $a->[
+$i] } },
iterator => sub { for( @{ $a } ) { $b = $_ } } } )
EOC
Benchmark: timing 2000 iterations of index, iterator...
index: 27 wallclock secs (21.21 usr + 0.02 sys = 21.23 CPU) @ 94
+.21/s (n=2000)
iterator: 6 wallclock secs ( 5.39 usr + 0.00 sys = 5.39 CPU) @ 37
+1.06/s (n=2000)
| [reply] [d/l] [select] |
#!/usr/bin/perl
use strict;
my %hash;
my $i;
$hash{"array"} = ['a','b','c','d','e'];
for ($i = 0; $i <= $#{ $hash{array} }; $i++) {
print "$i = $hash{array}[$i]\n";
}
davidj | [reply] [d/l] |
foreach (@{$hash{"array"}})
{
print "$_\n";
}
[]'s
-DBC | [reply] |
Use $# as any other sigil. As you may or may not know, you can use ${...}, @{...}, %{...} etc when dereferecing, but often the curlies are redundant which is why you don't see them so often. In fact, $foo can be written as ${foo} (which is an often seen trick when interpolating $foo in string like "this${foo}that" where $foothat would be interpolated if the brackets weren't there).
Anyway, in your case this means that you'd use $#{$hash{array}} and @{$hash{array}} to get the last element and the array itself, respectively.
See the perlref manpage for more details. Good reading is also to be found in perlreftut, perllol and perldsc
Hope I've helped,
ihb
| [reply] [d/l] [select] |