Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have defined array @route, where am storing 100 values. I need to access only 10 of them for a purpose. Is there a way to do this in perl

for (1..100) { <> push @route, $route_su; }

Am trying to get first 10 values like this,

 $route[1..10]  $route[1-10].

Both of them did not work.i do not want use any sort of loop command to achieve this

Replies are listed 'Best First'.
Re: Accessing multiple array index
by hippo (Archbishop) on Jan 17, 2016 at 16:48 UTC
Re: Accessing multiple array index
by 1nickt (Canon) on Jan 17, 2016 at 16:55 UTC

    "i do not want use any sort of loop command to achieve this"

    See: slice

    A slice accesses several elements of a list, an array, or a hash simul +taneously using a list of subscripts

    my @subset = @route[0..9];

    The way forward always starts with a minimal test.
Re: Accessing multiple array index
by GrandFather (Saint) on Jan 17, 2016 at 19:51 UTC

    A key to understanding Perl is that the sigils ($, @, %, ...) tell you the type of the thing that an expression returns. $route[1..10] returns a scalar value. @route[1..10] returns a list.

    The $route[1..10] case is particularly interesting because with strictures (you do always use warnings and strict, use strict; use warnings;, don't you?) you get a message like:

    Use of uninitialized value $. in range (or flip) at ...

    See Flipin good, or a total flop? for why that is.

    Premature optimization is the root of all job security
Re: Accessing multiple array indexes
by Athanasius (Archbishop) on Jan 18, 2016 at 03:07 UTC

    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,

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

Re: Accessing multiple array index
by hotchiwawa (Scribe) on Jan 17, 2016 at 16:38 UTC
    for(0..9) { print " ", $route[ $_ ]; } #or print " ", $route[ $_ ] for (0..9); #or perl -e 'my @a = qw(10 34 3 4 30 50 60 77); for(0, 2, 4, 6..7) { print + " ", $a[ $_ ]; }' perl -e 'my @a = qw(10 34 3 4 30 50 60 77); print " ", $a[ $_ ] for(0, + 2, 4, 6..7);' # will give: 10 3 30 60 77

      The op wrote "i do not want use any sort of loop command to achieve this". How do any of your "solutions" fit this criteria?

      Premature optimization is the root of all job security