http://qs1969.pair.com?node_id=169581


in reply to Re: foreach loops
in thread foreach loops

# Initialise an array so that $array[n] = n my $n = 0; foreach (@array) {$_ = $n++}
Your code is overwriting the contents of an already existing array, I wouldn't call that initialising ...

Putting that aside, an array slice is a good way to do that task without using a loop.

@array[0..100] = 0..100; # or to mimic the behaviour of your code exactly @array[0..$#array] = 0..$#array;

-- Hofmator

Replies are listed 'Best First'.
Re: Re: Re: foreach loops
by Bilbo (Pilgrim) on May 27, 2002 at 14:23 UTC
    Thank you - I've suddenly understood what array slices are for and how useful they are (I haven't been learning Perl for very long - I've certainly got a long way to go). I admit that I didn't choose a very good example. I think that my main point is still relevant though.