in reply to TIMTOWTDI doesn't mean invent an outlandish approach (usually)

There is more than one way to do it. But not every way is a good one. For example, you mention scalar(@array) - 1 to get the last index. This is not a good way to get the last index. It is a very bad way to get the last index.

Just like in real language, it isn't only about what you say, but also HOW you express that. scalar(@array) does not mean "the last index plus one" or even "the number that will be the index if you add a new element". It says "the number of elements".

It would be sort-of okay to use scalar(@array) - 1 if $#array did not exist. But $#array does exist, and it says "the index of the last element in @array".

$#arrayThe index of the last element
$#array + 1The index of the last element plus one
@arrayThe number of elements
@array - 1The number of elements minus one

For the same reason, $#array + 1 would be WRONG if you want to get the number of elements.

Sometimes, unless is better than if. Sometimes unless is wrong and if is right. Sometimes you need to reverse the expressions (open or die versus die unless open) to get the perfect sentence.

TIMTOWDTI. NYPAGO. (Now You Pick A Good One)

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

P.S. $[ is irrelevant. Yes, it would sort of support my point. But even without $[, using $#array+1 instead of @array, or @array-1 instead of $#array would be wrong when you use code that doesn't express what you meant.

  • Comment on Re: TIMTOWTDI doesn't mean invent an outlandish approach (usually)

Replies are listed 'Best First'.
Re: Re: TIMTOWTDI doesn't mean invent an outlandish approach (usually)
by welchavw (Pilgrim) on Oct 13, 2003 at 13:49 UTC

    It does not seem out of place that a quest for canonical form should drive monks. I tend to agree that all ways to express a solution are not equal. Perl is beautiful in its expressiveness, but it does not follow that a solution has value merely because it is excessively clever. Sometimes, other principles should guide. If you need a efficient solution, Perl can provide...but lo' you can get a playful take as well. The context of a problem should guide the monk's post as context guides Perl itself.

Re: Re: TIMTOWTDI doesn't mean invent an outlandish approach (usually)
by sandfly (Beadle) on Oct 17, 2003 at 20:48 UTC
    I feel compelled to reveal a guilty secret... I have an irrational dislike of $#array. It seems superfluous.

    Although I use "$array[$#array]" in preference to "$array[@array-1]", I don't enjoy it, and generally prefer to avoid the whole construction. And I always use "$i <= @array" instead of "$i < $#array".

      ...And I always use "$i <= @array" instead of "$i < $#array"...

      I hope not since they don't mean the same thing unless $[ = 2. However, please realize that $#array is actually:

      $#array = $[ - 1 + @array;

      In other words, if anyone ever sets $[ to anything other than the default 0, not only do they then deserve to be beaten with a rubber mallet but they also break your code. Of course, I'm one of those weirdos who does stuff like:

      my @acceptable = grep { check $array[$_] } ($[..$#array);

      antirice    
      The first rule of Perl club is - use Perl
      The
      ith rule of Perl club is - follow rule i - 1 for i > 1

      I hope I will never have to maintain your code. (Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.)

      Anyway... Why the hell would you use $array[$#array]? $array[-1] is so much easier.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Although I use "$array[$#array]" in ...

      Have you anything against $array[-1] ??

      Update: Juerd already said it. Must be time to go home...