Foreach

T

Ted Zlatanov

TZ> I really don't think it's such a big deal. Yeah, it's not ideal, but
TZ> it's hardly a crime against humanity. I really, really doubt it will
TZ> cause maintenance problems long-term (as long as you're sure of having
TZ> Perl 5.8 or later). And it's quite readable.

UG> readable or not, it is misleading to the reader. see my other post on that.

I disagree and your posts have not convinced me. Please note that
unlike you and most others in this thread, I'm giving a nuanced opinion.

TZ> So judge the audience for your code and write it accordingly. When in
TZ> doubt, be conservative, explicit, and simple.

UG> the audience judges your code, you don't judge the audience!

Please don't play word games. I clearly meant "judge" as in "to form an
opinion through careful weighing of evidence" not "to sit in judgment of."



EP> I've just checked. B<map> in void context is use-warnings clean. But
EP> one day it could change.

I try not to worry about that very unlikely event.

Ted
 
K

Keith Thompson

Keith Thompson said:
Uri Guttman said:
KT> No, in the code I posted I need to know when I'm processing the last
KT> item.

so use a while/shift loop instead. i stay away from indexing arrays as
much as possible as there is usually a better way to deal with things.

while( defined( my $elem = shift @array ) ) {

last_elem( $elem) unless @array ;

rest of code
}

Cool. Of course it destroys the array as you traverse it, which may or
may not be a problem.

[more good ideas snipped]
not too fugly. and better than indexing. perl6 will be able to loop over
indexes and elems together like each does for hashes.

Ah, yes, Perl6's "kv" operator seems to be just about exactly what
I was looking for. For example:

for @*ARGS.kv -> $index, $value {
say "index = $index, value = $value";
}
[...]

And in Perl 5.12 or higher, "each" can operate on arrays as well as
hashes:

require 5.012;

while (my($index, $value) = each @ARGV) {
print "index = $index, value = $value\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top