Dermot wrote:
> Hi,
> 
> I just spent 20mins scratching my head because I didn't read the docs
> very closely concerning splice. I had assumed it left the array
> intact, its doesn't. I was hoping for a function more akin to substr
> where the return would be the offset -> length of the array but the
> value it was working on remained in tact.
> 
> @array = (1, 4, 6, 9, 2);
> my @list = splice(@array, 1, 3);        # @list = (1, 4, 6) and @array =(9,2)
> 
> It doesn't say but do indexes start with 0 with splice?
> 
> Is there a List::Util or similar that might meet my needs? Or do I
> simple push the values back once I've used splice?
> push(@array, @list);
> 
> TIA,
> Dp.
> 

Use an array slice.  See `perldoc perldata` and search for /Slices/ or
http://perldoc.perl.org/perldata.html#Slices

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
$Data::Dumper::Maxdepth = 0;

my @array = (1, 4, 6, 9, 2);
my @list = @array[ 1 .. 3 ];

print Dumper \...@array, \...@list;


-- 
Just my 0.00000002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to