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

Hi Perl monks,

I'm trying to find the first element in a list using a foreach in order to do a different treatment:

 foreach $element (@{my_list})

what i want is something like:

foreach $element (@{my_list}) if ($element.fist) treat_1(); else treat_others();

Can anyone tell me how i can obtain the first element?

thanks

Replies are listed 'Best First'.
Re: Determine first element in foreach
by MidLifeXis (Monsignor) on Aug 04, 2014 at 18:24 UTC
    my ( $first, @list ) = @{my_list}; treat_1( $first ); treat_others( $_ ) for @list;

    If $my_list can be destroyed, it can be made shorter.

    treat_1( shift @$my_list ); treat_others( $_ ) for @$my_list;

    If it cannot be destroyed, it can still be done without the temporary variables:

    treat_1( $my_list->[0] ); treat_others( $my_list->[$_] ) for ( 1 .. $#{$my_list} );

    Update: Added a couple of more different implementations.

    --MidLifeXis

Re: Determine first element in foreach
by LanX (Saint) on Aug 04, 2014 at 18:06 UTC
    Check a flag with if($first) !

    Before you enter the loop you set my $first=1 and in the if-branch you do $first=0 to switch it off.

    HTH! :)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Determine first element in foreach
by frozenwithjoy (Priest) on Aug 04, 2014 at 18:34 UTC
    Yet another option:
    my @my_list = (...); treat_all(@my_list); sub treat_all { my ( $first, @others ) = @_; treat_1($first); treat_others($_) for @others; }
Re: Determine first element in foreach
by neilwatson (Priest) on Aug 04, 2014 at 18:01 UTC

    You can use a for loop instead of foreach

    for ($i = 0; $i < $#mylist ; $i++) { say "first element" if ( $i == 0 ); }

    Neil Watson
    watson-wilson.ca

      Even when you want to loop over the indexes, you can still use a foreach-style loop:

      foreach my $i (0 .. $#mylist) { say "first element" if ( $i == 0 ); }

      foreach-style loops are generally a better idea that C-style for loops. They're clearer; more declarative. It's harder to introduce an off-by-one error, and easier to spot a mistake.

        It's harder to introduce an off-by-one error, and easier to spot a mistake.

        Indeed. Consider
            for ($i = 0; $i < $#mylist ; $i++) { ... }
        in reply above:  $i < $#mylist vice  $i <= $#mylist — or did you have that in mind?

Re: Determine first element in foreach
by 1s44c (Scribe) on Aug 04, 2014 at 21:36 UTC

    Can anyone tell me how i can obtain the first element?

    To answer the question exactly as asked you can shift off the first element:

    my @array = qw/one two three/; my $x = shift @array;

    That will remove the first element from the array leaving 'two','three'. If you don't want to alter the list you can directly access the first element:

    my @array = qw/one two three/; my $x = $array[0];

      Thanks for the help everyone.

      I will take a look at all the answers and see which solution to implement.

Re: Determine first element in foreach
by runrig (Abbot) on Aug 04, 2014 at 21:25 UTC
    my $cnt; for my $element (@elements) { unless ($cnt++) { # deal w/first element next; } # Deal w/the rest }