SPUG: last index array reference's referent
Michael R. Wolf
MichaelRWolf at att.net
Sun Jan 21 15:21:31 PST 2007
# If you have an array ref and don't care about the index, it's pretty
# simple and elegant to loop over the referenced array...
@season = qw(winter spring summer fall);
$a_ref = \@season;
foreach my $element ( @{$a_ref} ) {
# Use $element here...
print "$element\n";
}
# But if you *do* care about the index, it's neither simple, nor
# elegant...
foreach my $i ( 0 .. @{$a_ref} - 1 ) {
my $element = $a_ref->[$i];
# Use $i and $element here...
print "$i $element\n";
}
# Is there a more elegant way to get the equivalent of $#seasons if
# all you have is a reference? Somehting that would do to references
# what $# does to arrays....
foreach my $i (0 .. $#season) {
my $element = $season[$i];
# Use $i and $element here...
print "$i $element\n";
}
--
Michael R. Wolf
All mammals learn by playing!
MichaelRWolf at att.net
More information about the spug-list
mailing list