Just another Perl shrine | |
PerlMonks |
Re^3: Extracting columns from a two dimensional arrayby Aristotle (Chancellor) |
on Dec 21, 2004 at 14:34 UTC ( [id://416480]=note: print w/replies, xml ) | Need Help?? |
That is an entirely different question. First off: please don't use for(;;) loops in Perl unless you really need to. For iterating over arrays or lists, there is almost never a good reason to favour this type of loop. Use foreach( LIST ) instead — that will very nearly remove the source for a large chunk of off-by-one errors. And make it a habit not to use doublequotes where you don't need them. It will bite you when stringification is not an identity function, such as with references, besides the fact that it forces needless work on the interpreter.
If you actually needed the index, you can do that too:
Of course in this case you don't. As to your question, well, something very similar can indeed be done:
Whether that's faster or not I don't know and don't care, and neither should you. I do know that it's not going to be measurably faster or slower either way, since both ways do roughly the same amount of work. Other factors are going to dictate the speed of your program anyway — if you read a lot of data from a file or pull any across a network, the penalty waiting for spinning metal or travelling electrons will outweigh microoptimizations like this one by so many orders of magnitude that the impact of for vs map won't be more than noise in your benchmarks. (You do benchmark, don't you?) And that's just two examples of the things that will eclipse this so much. So choose the idiom that you find easier to understand.
Makeshifts last the longest.
In Section
Seekers of Perl Wisdom
|
|