[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Elementary question (I think)



At 10.46 -0500 1999.03.11, Chris Sansom wrote:
>Oops - I think I sent this to the wrong address. Try again...

The old address is aliased to the new, so no worries.

>Suppose I have an array @stuff, when can and can't I use this syntax:
>
>for ($i = 0; $i <= @stuff, $i++) {
>...
>}
>
>I was under the impression that this was perfectly safe, but it seems to
>break my script. As soon as I change all those @'s in this type of
>construct to $# it works just fine.

@array is the number of elements in @array.  $#array is the last index of
@array.

@array = (0..9);
# scalar @array now is 10
# $#array now is 9

You want the last index, so you want 9.  You can also do this, however:

for my $i (0 .. $#array) {


}

Does the same thing, except that you preallocate a list of 0 .. $#array,
which in perls vefore 5.005 (including MacPerl, which is at 5.004) may
cause memory problems if $#array is huge.

[Also note that if @array is tied (see man perltie), then $#array and
scalar @array may not return reasonable values at all.  But I doubt you are
using a tied array.]

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org