Sunday, January 2, 2011

Perl: Splice function

Method 1:

@names=("Muriel","Sarah","Susanne","Gavin");

&look;

@middle=splice (@names, 1, 2);               #@names is removed index of 1 and 2 value. i. e. $names[1] and
                                                                 # $names[2] and Store in these two values are in @middle.
&look;

sub look {
        print "Names : @names\n";
        print "The Splice Girls are: @middle\n\n";
}


OUTPUT:

Names : Muriel Sarah Susanne Gavin
The Splice Girls are:

Names : Muriel Gavin
The Splice Girls are: Sarah Susanne

Method 2:

@names=("Muriel","Gavin","Susanne","Sarah");
@cities=("Brussels","Hamburg","London","Breda");

&look;

splice (@names, 1, 0, @cities[1..3]);

&look;

sub look {
        print "Names : @names\n";
        print "Cities: @cities\n";
}


OUTPUT:

Names : Muriel Gavin Susanne Sarah
Cities: Brussels Hamburg London Breda
Names : Muriel Hamburg London Breda Gavin Susanne Sarah
Cities: Brussels Hamburg London Breda

No comments: