Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Array splice

by code-ninja (Scribe)
on Aug 30, 2013 at 07:11 UTC ( [id://1051556]=perlquestion: print w/replies, xml ) Need Help??

code-ninja has asked for the wisdom of the Perl Monks concerning the following question:

This may seem a stupid question but one of the Monastery's quotes say that "A stupid question is a question not asked."

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.

#!/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";
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.

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
    Splicing with length 0 at offset 0 prepends before the first element.

    Splicing with length 0 at offset $#arr inserts before the last element.

    Splicing with length 0 at offset @arr inserts after the last element. Splicing at offset > @arr issues a warning:

    If OFFSET is past the end of the array, Perl issues a warning, and splices at the end of the array.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Array splice
by hdb (Monsignor) on Aug 30, 2013 at 07:20 UTC

    Well, if you just say

    my @array; $array[12] = 5;

    Perl silently assumes that you mean what you say and allocates sufficient memory to satisfy your wishes. (This has the potential downside that it allocates lots of memory and copies lots of data around if you are not careful.)

    Why do you refer to strcpy in your example? As far as I remember this copies strings and not arrays in C?

      Just to illustrate hdb's point that Perl's arrays are dynamic — and also that Perl supports negative indexing, which C doesn't like (other than a negative index of -1 for some reason, IIRC).

      >perl -wMstrict -le "my @array; print 'elements in array: ', scalar @array; ;; $array[1_000_000] = 42; print 'elements in array: ', scalar @array; ;; $array[-2] = 41;; print qq{elements at end of array: @array[ -2 .. -1 ]}; " elements in array: 0 elements in array: 1000001 elements at end of array: 41 42

      Update: Changed example code.

      I refer to strcpy because it was kinda analogous to what I wanted to ask that's all.
Re: Array splice
by DrHyde (Prior) on Aug 30, 2013 at 12:05 UTC

    The reason it doesn't segfault is the same as the reason you never have to malloc() or free() anywhere in perl code: perl handles all the memory allocation for you behind the scenes. Perl arrays (and strings, and hashes) will all grow to fit whatever you stuff into them.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1051556]
Approved by hdb
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-19 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found