Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: array splicing

by David Caughell (Monk)
on Jan 30, 2004 at 05:19 UTC ( [id://325140]=note: print w/replies, xml ) Need Help??


in reply to array splicing

Firstly, a slice is just a set of elements of an array. In other words, if you want the first 5 and last 5 members of a class of 100 people, you would write:

my @smalllist = @biglist[0 .. 4, 95 .. 99];

Note that while using a slice you access an array as a list (@) rather than a scalar($).

Using a slice keeps the @biglist the same as when you started. If you want to trim off the @biglist to eliminate the people in the @smalllist from it, you could use a splice() function call to do it.

my @smalllist = splice(@biglist,0,5), splice(@biglist,-5,5);

Mostly, the reason that you would use splice() is just because it's a quick way of getting rid of the elements of a list as you use them, especially when you're using them in a unusual order or in sizes other than 1.

The Offset means where you start your splice from, and either refers to a position away from the start of the list (if you use a positive number) or away from the end of the list (if you use a negative number).

Length is how many elements you're going to cut away from the list.

Hope this helps,
Dave.

P.S.
When I was first learning Perl, I was very excited about how flexible the functions were, and I read quite a bit of the perlfunc document. You might want to try that. Afterwards, though, I read Programming Perl (which I still don't fully understand). It's a challenging read, but worth it. If you only read up on the functions, you won't see the full power of the language.

You can read about the functions here: http://www.perldoc.com/perl5.8.0/pod/perlfunc.html

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-25 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found