Having said that, I'm working on making Perl my preferred programming language. Right now, I'm an accomplished C programmer but I want some change... whatever, back to the point.
the "splice" line is essentially push(@arr, @new) right? From C perspective, it is basically strcpy(arr, new) where "arr" and "new" are C arrays.#!/usr/bin/perl use strict; use warnings; my @arr = ( "hello to humans", "of planet Earth.", ); my @new = ( "I am here to annihilate you all so", "please co-operate. It is in accordance with the Galactic council. +", ); splice(@arr, scalar(@arr), 0, @new); print "@arr\n";
Again from a C perspective, let *(arr + n) (for some n) point to the location past the last entry of array "arr". If I say strcpy((arr + n), new) I'll get a seg fault (obviously!). But when I do splice(@arr, scalar(@arr), n, @new) I do not get the seg fault.
Why I think it should seg fault?Simple, scalar(@arr) takes me to the last position in the array "arr" and the third argument to splice is the length. Now, my length is exceeding the length of the array "arr" hence I'm trying to access the something out of bounds, therefore it should seg fault.
Whats the story, morning glory?!In reply to Array splice by code-ninja
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |