Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use splice function to add two arrays 1

Status
Not open for further replies.

dino25

Technical User
Aug 13, 2008
3
I would like to use the splice function to add two arrays together ..ie array1 and array3 without removing any elements.


 
push @array3, @array1



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
no, I want to use splice to insert array2 in the middle of array1
 
Did you read the docs on splice? perldoc -f splice
Code:
my @a1 = qw/0 1 2 3 4 8 9 10/;
my @a2 = qw/5 6 7/;

print "BEFORE: ", join(" ", @a1), "\n";
splice @a1, 5, 0, @a2;
print "AFTER: ", join(" ", @a1), "\n";
 
although this code makes good sense but it still would not put @a2 in the middle of @a1 but rather at the end. Even though the offset is right at the middle.
 
It works fine. Did you actually try it?

BEFORE: 0 1 2 3 4 8 9 10
AFTER: 0 1 2 3 4 5 6 7 8 9 10

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Star for rharsh!!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Travis - thanks for the star, and for checking my code! [thumbsup]
 
no, I want to use splice to insert array2 in the middle of array1

hehehe..... didn't you think that small detail was important to mention in your first post?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top