in reply to Is $# still used?
$# is useful when you need an explicit index, eg., 0 .. $#arr . Sure, it's more perlish to avoid the index and iterate over the memebers directly, but sometimes you need this, for example when accessing two related arrays. (Yes, I know. If they're related then they may have been associated together in a list of hashes. Sometimes that's not appropriate though.)
In Perl 6, the .kv method operates on arrays too, yielding numeric indexes and values:
say "what is your favorite color?"; my @favorite_colors = ("blue", "red", "uhh, yellow!"); for @favorite_colors.kv -> $id, $color { say "$id. $color"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is $# still used?
by Zaxo (Archbishop) on Jun 11, 2005 at 10:10 UTC | |
by gaal (Parson) on Jun 11, 2005 at 13:27 UTC |