Im programming in Perl, can I replace an element in an array with two new elements?
January 21, 2012 – 5:22 pmFor example, if I had the array @values=(1,2,3,4) is it possible that I replace the 2nd element with two new elements of 5 and 6; so @values would now equal (1,5,6,3,4)? And I don’t care which number element the 5 and 6 take.
Related posts:
- one to many elements in an array in perl?
- Deleting a specific Element from a Perl Array?
- I want to perform a calculation in Perl that should be done with every element in an array. Help?
- I want to perform a calculation in Perl that should be done with every element in an array. Help?
- How can i sum all elements of an array in perl?
- In Perl, how do I add elements of an array using a loop?
- In Perl, how do I add elements of an array using a loop?
- How do I Compare 2D Array Elements in Python?
- Simple Perl array/for loop question…?
- simple way to see if an element exists in a ruby array?
One Response to “Im programming in Perl, can I replace an element in an array with two new elements?”
splice @values, 1, 1, 5, 6;
The arguments to splice are:
array – the array to operate on
offset – the offset at which to start removing elements (1 = second element)
length – the number of elements to remove
list – replacement elements (5, 6)
This program:
===================
#!/usr/bin/perl
@values = (1, 2, 3, 4);
splice @values, 1, 1, 5, 6;
print @values, "\n";
===================
… prints:
===================
15634
===================
That’s what you want, right?
@M
By McFate on Jan 21, 2012