in reply to Check for next array element.

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++; }

Replies are listed 'Best First'.
Re^2: Check for next array element.
by rashley (Scribe) on Nov 18, 2005 at 15:52 UTC
    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.
Re^2: Check for next array element.
by BaldPenguin (Friar) on Nov 18, 2005 at 18:47 UTC
    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?! :-)