logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

Re: Counting operator (was Re: [PERL-QOTW-ADMIN] Why no quizzes this week?: msg#00152

Subject: Re: Counting operator (was Re: [PERL-QOTW-ADMIN] Why no quizzes this week?)
Xavier Noria wrote:
> 
> I would expect the range operator to work on both directions.
> 
> I have no objective reasons to support that, but it feels just natural 
> to me to be able to say 9..0 given that we are able to say 0..9. Maybe 
> it's kind of completeness, or symmetry, I am not sure, but I prefer it.
> 
> Which are the arguments against it?

Umm, I included this in my original message:

>   for my $i (0..$#array) {
>     # do something
>   }

For example:

  sub printall {
    my @array = @_;

    for my $i (0..$#array) {
      print "Element #$i is $array[$i]\n";
    }
  }

ie, cases where you loop through an array but need the index in
addition to the value.

  printall ("a", "b", "c");

works fine:

  Element #0 is a
  Element #1 is b
  Element #2 is c

but consider that:

  printall ();

would yield:

  Element #0 is         --> uh oh... reference to an undef on
  Element #-1 is            both of these lines

If '..' always counted both ways, you'd need to check the size of
@array before the for loop, which seems like a step backwards.

--
Ron Isaacson
Morgan Stanley
ron.isaacson-/PgpppG8B+R7qynMiXIxWgC/G2K4zDHf@xxxxxxxxxxxxxxxx / (718) 754-2345

NOTICE: If received in error, please destroy and notify sender.  Sender
does not waive confidentiality or privilege, and use is prohibited.



<Prev in Thread] Current Thread [Next in Thread>