in reply to Getting the next array element while still keeping the current one

Sometimes what I do for this is:
my @array = qw( wilma fred barney betty ); foreach my $i ( 0 .. $#array ) { print "$array[ $i ] is the current element.\t"; print "The next element is "; # Before using the next element, test for its existence. print exists $array[ $i + 1 ] ? $array[ $i + 1 ] : "unprintable" ; print ".\n"; # An approach more to your liking might be: # if ( exists $array[ $i + 1 ] ) { # or: # last if $i == $#array; # or: # print $array[ $i + 1 ] unless $i == $#array; # }

Other times, like after I've already written my loop but I need to expand its powers to be able to access the next element, I might do:

use strict; my @array = qw( wilma fred barney betty ); my $ctr = 0; my $next_name; foreach my $name ( @array ) { print "$name is the current element.\t"; # Before using the next element, test for its existence. $next_name = exists $array[ $ctr + 1 ] ? $array[ $ctr + 1 ] : "unavailable" ; # Or you could break out immediately or perhaps have some # "end of array" code: # last if $next_name eq 'unavailable'; # or: # if ( $next_name eq 'unavailable' ) { # # do whatever you do when no more elements # } print "The next element is $next_name.\n"; } continue { $ctr ++; } # Use of the '} continue {' line is more fancy than necessary; # It's useful if you plan to break out of your loop with # a 'next' command.

Note that while and for have their own ways of letting you set up these loops. I'm just in something of a rut of using foreach (which could be shortened to for) and twist things as above in order to use its approach.

Replies are listed 'Best First'.
Re: Re: Getting the next array element while still keeping the current one
by NetWallah (Canon) on May 02, 2004 at 14:26 UTC
    These code samples, while correct, are tending to get long, and not easily remembered as idioms.

    Here is another attempt at making this readable and small ..(And , hopefully, correct)

    my @a=qw( wilma fred barney betty pebbels dino ); for (my ($i,$p,$c,$n)=(0,undef,@a); $i < scalar @a; $i++,($p,$c,$n) = ($c,$n,$a[$i+1])){ print qq($i Prev=$p \tCurr=$c \tNext=$n\n) } -- Output -- 0 Prev= Curr=wilma Next=fred 1 Prev=wilma Curr=fred Next=barney 2 Prev=fred Curr=barney Next=betty 3 Prev=barney Curr=betty Next=pebbels 4 Prev=betty Curr=pebbels Next=dino 5 Prev=pebbels Curr=dino Next=
    Update:Noticed and Fixed Bug: Changed <= to <, @a[[$i]] to @a[[$i+1]].

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarntees offense.

      That's one clear piece of code (w.r.t. doing everything in/around the main loop).

      BTW, did you intentionally removed the warnings pragma or just forgot?

        I'm responding to the minor gripes that my code whines about "Use of uninitialized value in concatenation (.) or string..." under use warnings;.

        The only place where it complains is the print statement, which was intended to be a sample - the print was to be replaced by real code.

        The undef values in $p and $n are ligitimate for those boundary conditions.

        Offense, like beauty, is in the eye of the beholder, and a fantasy.
        By guaranteeing freedom of expression, the First Amendment also guarntees offense.