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

I have a loop that iterates through an array performs various hash functions.
foreach $_ (@list) { DO STUFF }
One of the things that I need to do in that loop is set a flag that indicates whether or not there are any other elements in the array.

Something like:

if (???) { $data->{'IS_LAST'} = false; } else { $data->{'IS_LAST'} = true; }
So, what would go in the ??? ?

Thanks

Replies are listed 'Best First'.
Re: Check for next array element.
by Fletch (Bishop) on Nov 18, 2005 at 15:45 UTC

    If that's what you need you need to either use a C style for or iterate over the array indices instead of the array itself.

    for my $i ( 0 .. $#list ) { $data->{"IS_LAST"} = $i < $#list ? 0 : 1; ## ... }

    Another alternative would be to iterate over all items save the last and then process the last item seperately.

    for my $item ( @list[ 0 .. $#list - 1 ] ) { process_item( $item, 0 ); } process_item( $list[ $#list ], 1 );
Re: Check for next array element.
by Aristotle (Chancellor) on Nov 18, 2005 at 15:45 UTC

    You can’t do that. For that case I suggest a foreach over the array’s indices:

    foreach my $i ( 0 .. $#list ) { local $_ = $list[ $i ]; # ... $data->{ IS_LAST } = ( $i == $#list ); # ... }

    Makeshifts last the longest.

Re: Check for next array element.
by friedo (Prior) on Nov 18, 2005 at 15:46 UTC
    If you keep a counter variable, you can check to see if it's the last element. For example:

    my $count = 1; my $size = @list; foreach( @list ) { # DO STUFF $data->{IS_LAST} = $count == $size ? 1 : 0; $count++; }
      Thanks, that's what i was looking for.

      I know this was easy, but I'm not familiar with the

      $data->{IS_LAST} = $count == $size ? 1 : 0;
      syntax.
        That is the ternary conditional operator, which is short for if/then/else. I like to use it for assigning a conditional value. If the condition $count == $size is true, then 1 is returned, otherwise 0.
      Couldn't that also be written as
      $data->{'IS_LAST'} = 0; for ( my $i = 0; $i < @list; $i++ ) { # DO STUFF $data->{'IS_LAST'} = 1 if $i == $#list; }

      Don
      WHITEPAGES.COM | INC
      Everything I've learned in life can be summed up in a small perl script!

        Too easy to make off-by-one mistakes that way. Use for my $i ( 0 .. $#list ) { ... } instead, unless you have speficic reason to use for(;;). (I have never had such an occasion, but maybe you will.)

        Makeshifts last the longest.

        Sure, but then in the "DO STUFF" section you have to work with $list[$i] instead of $_. What are we, animals?! :-)
Re: Check for next array element.
by Eimi Metamorphoumai (Deacon) on Nov 18, 2005 at 15:53 UTC
    Yet another suggestion: go through the loop and tell ever item (including the last one) that it's not the last. Then after the main loop, change the value for the last item. It's not as direct as some of the other suggestions, but if there's a lot of other stuff in the loop, it might be the simplest.