Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

array question

by gogoglou (Beadle)
on Jun 30, 2010 at 13:46 UTC ( [id://847332]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, this might be a stupid question, but is there a loop structure by which I can get all the elements of an array, apart from the first one ($array1) ? Thanks in advance for any help

Replies are listed 'Best First'.
Re: array question
by kennethk (Abbot) on Jun 30, 2010 at 13:54 UTC
    I assume by skipping the first element, you mean $array[0], not $array[1]. I also not that you should read Writeup Formatting Tips to keep your array indices from being linkified.

    There is no generic loop structure that just skips the first element. You can however use loop control like next to skip undesirable elements:

    my $skip = 0; for my $i (0 .. $#array) { next if $i == $skip; #process }

    You can also you array slices in order to feed restricted sections of your array to a foreach loop:

    for (@array[1 .. $#array]) { #process }

    or if you have no need for the loop itself, you can use the slice with a direct assignment:

    my @new_array = @array[1 .. $#array]; or if you meant skipping $array[1]

    my @new_array = @array[0, 2 .. $#array];

    You can do more advanced list preprocessing with map and grep. After all, TIMTOWTDI.

Re: array question
by Corion (Patriarch) on Jun 30, 2010 at 13:48 UTC

    Do you want all elements of the array at once? Then use @array. Or do you want to process each element of the array? Then use

    foreach my $element (@array) { print "I am processing the element '$element'\n"; };

    You might want to see perlsyn.

    Further reading would be map if you want to transform elements of one array into another list.

Re: array question
by AndyZaft (Hermit) on Jun 30, 2010 at 14:04 UTC
    You mean you want to discard the first array element? In which case one way to do it is:
    for ($i = 1; $i < 10; $i++) { print $array[$i]; }
    Starting at 1 because first element's index is 0.
Re: array question
by Taulmarill (Deacon) on Jun 30, 2010 at 13:48 UTC

    Do you mean something like this?

    for my $element ( @array ) { print $element . "\n"; }
Re: array question
by rowdog (Curate) on Jun 30, 2010 at 21:46 UTC

    You can shift the first element off the array.

    my $first_element = shift @array;
Re: array question
by roboticus (Chancellor) on Jun 30, 2010 at 22:26 UTC

    gogoglou:

    You can use the slice syntax to get a set of array elements:

    my @array = (0 .. 50); my @first5 = @array[0..4]; my @allButFirst = @array[1..$#array]; my @FirstThirdAndEighth = @array[0,2,7];

    For details, read perldoc perldata and look for "slice" to find information about array and hash slicing.

    ...roboticus

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-05-30 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found