in reply to How do you define "elegant"?

i have just another example of elegance.

before:

my $num_count_expressions = @$count_expressions; my $count_expression_index; for($count_expression_index = 0; $count_expression_index < $num_count_expressions; ++$count_expression_index) {

after:
for my $count_expression_index (0..$#$count_expressions) {

Replies are listed 'Best First'.
Re^2: How do you define "elegant"?
by Limbic~Region (Chancellor) on Aug 17, 2006 at 17:33 UTC
    tinita,
    My response has nothing to do with elegance and you are quite right in your post. I do want to point out that the two snippets are not functionally equivalent.

    In the first inelegant snippet, the index var continues to exist beyond the for loop. In the second elegant snippet, it does not. This is because in Perl 5, an implicit block is defined. Last I heard, this will not be the case in Perl 6 and these two snippets would be functionally equivalent. I would have written it this way in Perl 6:

    for 0 .. $exprs.end -> $index { # ... }

    Cheers - L~R