Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

[splice], explanation please?

by @rocks (Scribe)
on Nov 16, 2002 at 07:11 UTC ( #213368=perlquestion: print w/replies, xml ) Need Help??

@rocks has asked for the wisdom of the Perl Monks concerning the following question:

Hey Perlmonks,

I need your help. Lately BrowserUK, Zaxo, and tye have been telling me about a command structure called splice. I have an older friend who programs for a living and says he knows a lot about Perl. I asked him about splice and he said he had no clue what that was.

I was just wondering if any monk would please be kind enough to write a little explanation of spice or to /msg me about it. :-) Thank you.

-@rocks

Replies are listed 'Best First'.
Re: [splice], explanation please?
by bart (Canon) on Nov 16, 2002 at 10:52 UTC
    Hmm... I was involved in a discussion on splice the other day, but I'm sure it wasn't with you... Anyway, IMO, splice() is one of the more difficult to use built-in functions in Perl.

    OK, here's the basic gist: splice is a bit to arrays, what substr is to strings. You can cut out a section of an array, and replace it with something else. There's no way to just get an excerpt from an array, and leaving it alone: use an array slice for that. To add to the confusion, the two words look much alike, don't they? I'll come back to slices later.

    So here's the basic syntax. We'll make an array containing 9 lower case letters. Then we'll extract two items, skipping the first 3, so taking the fourth and fifth item, and replace them with 3 other items, each an uppercase letter.

      Excelent, a good explination of the function. In my opinion you should consider putting this in the "Tutorials" section.

(jeffa) Re: splice, explanation please?
by jeffa (Bishop) on Nov 16, 2002 at 17:01 UTC
    splice is great. Here is how you can remove a whole column from a two-dimensional array:
    use strict; use Data::Dumper; my @table = ( [qw(a b c)], [qw(d e f)], [qw(g h i)], ); my @col = map { splice @$_,1,1 } @table; print Dumper \@col;
    The end result is the middle column of @table is removed and assigned to @col.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: [splice], explanation please?
by sauoq (Abbot) on Nov 16, 2002 at 07:15 UTC

    The most important thing to learn about splice (and most perl functions for that matter) is that the documents for it come along with perl itself. Try typing: perldoc -f splice

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: [splice], explanation please?
by nothingmuch (Priest) on Nov 16, 2002 at 07:16 UTC
    There's always perlfunc... splice takes an array for the first argument, and then removes 3rd argument elements from the 2nd argument and on, replacing them with the list in the 4th - inf argument, and returning the extracted list.
    my @array = qw(i do not like carrot); print "@array\n"; # you know; my @carrot = splice(@array,2,3,(qw(mow the lawn))); print "@array but @carrot\n";


    -nuffin
    zz zZ Z Z #!perl
Re: [splice], explanation please?
by bamy787 (Novice) on Nov 17, 2002 at 13:09 UTC
    splice ARRAY, OFFSET, LENGTH, LIST splice ARRAY, OFFSET, LENGTH splice ARRAY, OFFSET This function removes the elements designated by OFFSET and LENGTH fro +m an array, and replaces them with the elements of LIST, if any. The +function returns the elements removed from the array. The array grows + or shrinks as necessary. If LENGTH is omitted, the function removes +everything from OFFSET onward. The following equivalences hold (assum +ing $[ is 0): Direct Method Splice Equivalent --------------------------------------------------- push(@a, $x, $y) | splice(@a, $#a+1, 0, $x, $y) pop(@a) | splice(@a, -1) shift(@a) | splice(@a, 0, 1) unshift(@a, $x, $y) | splice(@a, 0, 0, $x, $y) $a[$x] = $y | splice(@a, $x, 1, $y); The splice function is also handy for carving up the argument list pas +sed to a subroutine. For example, assuming list lengths are passed be +fore lists: sub list_eq { # compare two list values my @a = splice(@_, 0, shift); my @b = splice(@_, 0, shift); return 0 unless @a == @b; # same len? while (@a) { return 0 if pop(@a) ne pop(@b); } return 1; } if (list_eq($len, @foo[1..$len], scalar(@bar), @bar)) { ... } It would probably be cleaner just to use references for this, however.
Re: [splice], explanation please?
by Anonymous Monk on Nov 17, 2002 at 02:12 UTC
    what the heck is a command structure?
Re: [splice], explanation please?
by Mur (Pilgrim) on Nov 19, 2002 at 14:52 UTC
    I wrote a gentle introduction to slices and splice() way back when for "PerlMonth".
    --
    Jeff Boes
    Database Engineer
    Nexcerpt, Inc.
    vox 269.226.9550 ext 24
    fax 269.349.9076
     http://www.nexcerpt.com
    ...Nexcerpt...Connecting People With Expertise

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://213368]
Approved by Zaxo
Front-paged by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2022-10-05 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (25 votes). Check out past polls.

    Notices?