code-ninja has asked for the wisdom of the Perl Monks concerning the following question:
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?!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array splice
by choroba (Cardinal) on Aug 30, 2013 at 07:25 UTC | |
|
Re: Array splice
by hdb (Monsignor) on Aug 30, 2013 at 07:20 UTC | |
by AnomalousMonk (Archbishop) on Aug 30, 2013 at 16:28 UTC | |
by code-ninja (Scribe) on Aug 30, 2013 at 07:27 UTC | |
|
Re: Array splice
by DrHyde (Prior) on Aug 30, 2013 at 12:05 UTC |