Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Re: foreach loops

by Hofmator (Curate)
on May 27, 2002 at 14:02 UTC ( [id://169581]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://169581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-25 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found