in reply to inserting elements in an array
This does what you ask:
#!/usr/bin/perl my @fred = ( "a", "b", "c" ); splice @fred, 2, 0, "hello"; print "@fred\n";
splice() takes four arguments: the array, the position to start at, the length of the sub-list to replace, and the list to replace it with. That sub-list length is the tricky part: make it 0 to insert things into the array. Any ordinal number "overwrites" that many elements in the array. For instance, make the third argument 1 and you'll see the "c" disappear since it's replaced.
|
|---|