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

Being the fledgling Perl Programmer that I am and trying to learn about the "splice" function, I searched the Monastery for "splice". Then being somewhat confused about the description I searched my handy/dandy Programming Perl book and found the same equally cryptic text. I thought that I would use "splice" to remove elements from an array. But, I'm getting confused as to the syntax. Below is a small program that I wrote to test the function.


#!/usr/bin/perl -w # # this is a test of the splice command # # # use strict; my $x = "how about this"; my $y = "this is the new line"; open (IN_FILE, "test.dat") or die "test.dat will not open: $!\n"; my @ary = <IN_FILE>; close (IN_FILE) or die "test.dat will not close: $!\n"; # print "Print the array before the change:\n @ary\n"; my $z = splice (@ary,$x,2); print "Printing the array:\n @ary\n\n\n"; print $z;


This is the output:

Print the array before the change:
this is a column|this is another|this is a also on
this could be|a column is this|and so is this
how about this|or maybe this| of this

Argument "how about this" isn't numeric in splice at ./test.pl line 16.
Print the array after the change:
how about this|or maybe this| of this



Print the splice value:this could be|a column is this|and so is this

I must be missing the point of the function.

"The Universe is not only more complex than we imagine, it is more complex than we can imagine." - Albert Einstein

Replies are listed 'Best First'.
(jeffa) Re: A Splice of life
by jeffa (Bishop) on Nov 30, 2001 at 22:43 UTC
    All perl built-in functions have documetation. On your command line type this:
    perldoc -f splice
    and you will see this:
    splice ARRAY,OFFSET,LENGTH,LIST splice ARRAY,OFFSET,LENGTH splice ARRAY,OFFSET splice ARRAY
    your $x needs to be an 'int' not a 'string', and $z should be an array - @z (splice returns an array - you could catch the output as a scalar to see how many lines were spliced - but usually you want the array back).

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: A Splice of life
by dragonchild (Archbishop) on Nov 30, 2001 at 22:45 UTC
    Directly from Programming Perl:
    splice ARRAY [OFFSET [LENGTH [LIST]]]

    What does that mean? Well, ARRAY and LIST are arrays. OFFSET and LENGTH are numbers. 'how about this' isn't a number, so splice doesn't know what to do with it.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Edit: chipmunk 2001-11-30

Re: A Splice of life
by seanbo (Chaplain) on Nov 30, 2001 at 23:01 UTC
    I think this is what you want:
    my $z = splice (@ary,2,0,$x);
    This will insert "how about this" at position 2 of the string and not remove any chars (the 0). You could check here also. Good luck.

    perl -e 'print reverse qw/o b n a e s/;'
Re: A Splice of life
by void (Scribe) on Nov 30, 2001 at 22:55 UTC
    I'm not really getting what Offset and Length are?
    How do they relate to the info in the array?

    "The Universe is not only more complex than we imagine, it is more complex than we can imagine." - Albert Einstein
      The offset is the number of elements to 'move over' from the front of the array, or the index of the array to start from (however you want to look at it). The length is the number of elements that you want to operate on from that point.

      Ie offset=2 and length=4 would operate on elements 3 through 6 the array. (element number 1 is offset or index 0)

      /\/\averick
      perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

        Thanks, makes more sense now.

        "The Universe is not only more complex than we imagine, it is more complex than we can imagine." - Albert Einstein