Elgon has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
on a similar point to Limbic~Region's (sounds like a Culture shipname to me) regarding array indices, I have a question on this one: Let's say that I have an array from which I wish to remove a record (in the sense of splice - like a DNA base deletion.) I am using the following code:
for ($index = 0; $index <= (scalar(@array) - 1); ++$index) { if ($array[$index] == $whatever) { splice ("array, $index, 1); } }
Or it could be done this way, I suppose...
$index = 0; foreach $thing(@array) { if ($thing == $whatever) { splice (@array, $index, 1); } ++$index; }
Personally I find the for loop somewhat messy and would prefer a tidy foreach instead, however the second way seems equally crufty. Is there simply a far more elegant way of doing this of which I am unaware? Limbic~Region's hypothetical function does do what I want but I was wondering if there was some *magic variable* used by Perl as an iterator in foreach loops somewhat like that found in the each function for use with hashes.
Of the solutions so far, I prefer the first but am very willing to listen to other ideas.
"Rule #17 of Travel: Never try and score dope off Hassidic Jews while under the impression that they are Rastafarians."
- Pete McCarthy, McCarthy's Bar
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Yet More fun With Array Indices
by japhy (Canon) on Sep 14, 2002 at 16:48 UTC | |
Re: Yet More fun With Array Indices
by blokhead (Monsignor) on Sep 14, 2002 at 16:51 UTC | |
by Aristotle (Chancellor) on Sep 14, 2002 at 17:01 UTC | |
by graff (Chancellor) on Sep 15, 2002 at 03:34 UTC | |
by Elgon (Curate) on Sep 14, 2002 at 18:51 UTC | |
Re: Yet More fun With Array Indices
by elusion (Curate) on Sep 14, 2002 at 16:47 UTC |