CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Use splice to add

 



colezka
New User

Apr 13, 2014, 10:00 AM

Post #1 of 3 (2091 views)
Use splice to add Can't Post

Hello,
I have small question about use splice to add something in a middle of a table:


Code
#!/usr/bin/perl 

@array = qw( pebbles dino fred barney betty );
@removed = splice @array, 1, 0, qw(wilma);
print "@removed\n";


STDOUT isn't be:


Quote
pebbles wilma dino fred barney betty


?

In my terminal i have only \n printed, nothing else.


Laurent_R
Veteran / Moderator

Apr 13, 2014, 11:58 AM

Post #2 of 3 (2087 views)
Re: [colezka] Use splice to add [In reply to] Can't Post

Your code works perfectly, except that you should try to print @array instead of @removed:

Code
print "@array";

which should print:

Code
pebbles wilma dino fred barney betty

The splice function:

Code
splice @array, offset, [length, [list]]

removes the elements of @array designated by offet and length and replaces them with list, if specified. Returns the elements that we removed.

Your length is 0, therefore no element is removed and @removed is empty. But 'wilma' has been added to the original @array.


(This post was edited by Laurent_R on Apr 13, 2014, 11:59 AM)


colezka
New User

Apr 13, 2014, 12:45 PM

Post #3 of 3 (2079 views)
Re: [Laurent_R] Use splice to add [In reply to] Can't Post

Thank you for good explain. Everything's clear.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives