>>>>> "PN" == Palit, Nilanjan <[EMAIL PROTECTED]> writes:
PN> Uri,
PN> Using both the array element value & index in a loop is a very
PN> fundamental coding usage (just like using the hash key & value), which I
PN> didn't think needed further elaboration (it's not exactly an esoteric
PN> usage model!). Nevertheless, here is an example I'm currently working
PN> on (which prompted the question):
please, i do know a little about looping as i am very loopy most of the
time! :)
my point is that in perl (but not in almost any other lang), loop
indexing is rarely needed. i have written thousands of lines of
production perl and i can barely count the times i use loop indexes. i
always design my data structures to not need them as it is cleaner and
faster to not use them. in most other langs loop indexes are practically
required as you don't have a proper loop over a list construct like
foreach.
PN> I'm parsing a Verilog assign statement & the RHS is a multi-bit
PN> concatenated bus. The key on the LHS includes the "[$i]", so I can use
PN> that to correlate the same signal to another place in the code:
PN> my $i= 0;
PN> foreach (@sigs)
PN> { $mybus{"bus1[$i]"}= $_; $i++; }
that can be done with for modifier and a range assuming @sigs can be
destroyed with shifts:
$mybus{ "bus1[$_]" } = shift @sigs for 0 .. $#sigs ;
here is another way to do it:
@mybus{ map "bus1[$_]", 0 .. $#sigs } = @sigs ;
shorter and non-destructive on @sigs. i like slices as another way to
avoid loop indexing.
seeing ++ on a loop index var is also a red flag that tells me to use a
range operator if possible. then you don't need to worry about ++ being
in the right place and you can put the loop control in a better place.
PN> I think it'd be fairly easy for Perl to auto initialize & increment a
PN> loop index in all loops & provide that to the user in a special
PN> variable. "$." is an excellent example. I think it'd be a great addition
PN> to Perl's excellent (& long) list of special vars, making for yet more
PN> elegant & concise code.
ain't gonna happen in perl5. perl6 has such a beast.
uri
--
Uri Guttman ------ [EMAIL PROTECTED] -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm