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

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.

Replies are listed 'Best First'.
Re: Re: Re: Getting the next array element while still keeping the current one
by parv (Parson) on May 02, 2004 at 18:23 UTC

    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.

        Yes, no question about the fact of two values being undefined. I just wanted to clear the things, at least for myself; thanks.