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".
| $#array | The index of the last element |
| $#array + 1 | The index of the last element plus one |
| @array | The number of elements |
| @array - 1 | The 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.
|
|---|
| 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 | |
|
Re: Re: TIMTOWTDI doesn't mean invent an outlandish approach (usually)
by sandfly (Beadle) on Oct 17, 2003 at 20:48 UTC | |
by antirice (Priest) on Oct 18, 2003 at 12:47 UTC | |
by Juerd (Abbot) on Oct 17, 2003 at 22:06 UTC | |
by runrig (Abbot) on Oct 18, 2003 at 01:17 UTC |