It was thus said that the Great A. Pagaltzis once stated: > * Sean Conner <s...@conman.org> [2006-12-22 21:35]: > > But *neolithic code*? > > Yeah. > > > What? I should do > > > > map > > { > > open INPUT $_; > > &do_some_process; > > close INPUT; > > } @ARGV; > > Almost. You can do it that way if you prefer it, but in cases > such as this one I'd write it like so: > > for ( @ARGV ) { > # ... > }
Ah, the Bronze Age code (more on this in a bit). > Or you can call it `foreach` if you like that better. > Independently of that, if you don't like the `$_`, you can name > a variable explicitly: > > foreach my $file ( @ARGV ) { > # and use $file here > } > > There is never a good reason to use `for(;;)` in Perl. If you > need to iterate over indices, you do it this way: > > for my $i ( 0 .. $#ARGV ) { > # ... and use $ARGV[ $i ] here > } > > As a side effect, you get to lose the `<` vs `<=` source of > off-by-one errors. I think this makes Peter daSilva's argument about Perl and it's "There Is More Than One Way To Do It" paradigm. Let's see ... for ( <init> ; <cond> ; <incr> ) for ( <max-count> ) for <var> ( <lower-bound> .. <upper-bound> ) No wonder it's taking so long for Perl 6 to come out---the parser alone must be horribly complex to disambiguate all these constructs (and from other messages, the Parl 6 parser is even *more* complex). Anyway, the bit about your code being the Bronze Age. If the way I do it is neolithic, your's isn't much better. It still implies an order to the operations, whereas map doesn't have that restriction and the code can be parallelized easier, and thus is a more modern way of programming, especially given the rise of multicore processors in even home PCs. That's why I picked that particular example. > But there is relatvely rarely even a need to index into an array. > Most of the time you just want to either iterate over the whole > thing or push/pop/shift/unshift elements. Makes code easier to > read, too, and reduces coupling even at the most local scales. Perhaps for what you do. I just checked some Perl code I wrote and I hit specific spots in multiple arrays and can't see a way around that (LOG refers to a webserver log file): while(<LOG>) { my @entry = m/$c_parse_apache/o; my $size = @entry; if ($size) { $g_hits++; $g_transfer += $entry[LOG_BYTES] unless $entry[LOG_BYTES] eq "-"; $g_hits[$entry[LOG_STATUS] / 100]++; &sort_entry(@entry); } } If you have a better way of coding this, I'm all ears (in fact, if you want the entire script, I'm happy to send it to you ... ) -spc (And don't ask me how the data is structured---I don't recall, since Perl lacks proper structures ... )