Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Pop/shift/delete on array

by monarch (Priest)
on Jul 14, 2005 at 08:34 UTC ( [id://474795]=note: print w/replies, xml ) Need Help??


in reply to Pop/shift/delete on array

let's say you have an array that looks like this

my @vals = ( 'cat', 'dog', 'mouse' );

Let's push onto this array:

push( @vals, 'tiger' ); print( join(',',@vals ) );
outputs cat,dog,mouse,tiger.

Let's pop from this array:

print( pop( @vals ) );
outputs tiger.

Let's shift from this array:

print( shift( @vals ) );
outputs cat.

Note that both shift and pop remove the value from the array that you obtained:

print( join(',',@vals ) );
prints dog,mouse.

Finally delete isn't an array function, it's used for deleting a hash element... update: whoops Limbic~Region has pointed out I was wrong about the delete function.. read his comments below.

Replies are listed 'Best First'.
Re^2: Pop/shift/delete on array
by Limbic~Region (Chancellor) on Jul 14, 2005 at 12:34 UTC
    monarch,
    Finally delete isn't an array function, it's used for deleting a hash element.

    While I have yet to see a good use for using delete on an array, this is misinformation. The docs for delete clearly indicate it can be used for arrays and array slices though it behaves differently then if it had been performed on a hash. It is designed to work with exists on array elements which I find just as icky. If the element isn't at the either end, I prefer to use splice to get rid of it even though I hear the cost of splicing an array is proportional to the size of the array at time of splicing.

    Cheers - L~R

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found