in reply to Accessing multiple array index

Just for the sake of completeness:

If you want to access the first 10 values of the array, and leave them there, then an array slice is the way to go, as explained by hippo, 1nickt, and GrandFather.

But if you want to remove the first 10 elements into a second array, and then access them, use Perl’s splice function:

#! perl use strict; use warnings; use Data::Dump; # Populate @route with 100 strings: 'aa', 'ab', 'ac', ... 'dv' my $string = 'aa'; my @route; push @route, $string++ for 1 .. 100; # Remove and print the first 10 elements my @first_ten = splice @route, 0, 10; dd \@first_ten; # Print the remaining elements dd \@route;

Output:

13:05 >perl 1517_SoPW.pl ["aa" .. "aj"] ["ak" .. "dv"] 13:05 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Accessing multiple array indexes
by Anonymous Monk on Jan 18, 2016 at 06:42 UTC

    Thanks for all your inputs. Splice did what i actually want to do.