Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^3: Editing Listed Data (index, stack)

by Anonymous Monk
on Mar 25, 2013 at 01:03 UTC ( #1025211=note: print w/replies, xml ) Need Help??

in reply to Re^2: Editing Listed Data
in thread Editing Listed Data

How Do I iterate through the array, yet compare the next item against the former?

You use an index, or you keep a stack

The index approach, its missing boundary checking ( ; beware of off-by-one errors

my @stuff = 0 .. 9; for my $ix ( 0 .. $#stuff ){ my $item = $stuff[$ix]; my $prev = $stuff[$ix-1]; my $next = $stuff[$ix+1]; }

The stack approach

my @stuff = 0 .. 9; my $lastitem = ""; for my $item ( @stuff ){ my $next = $item; if( $item eq $lastitem ){ print "they measure up\n"; } $lastitem = $item; }

Oh, but you $lastitem is not an array, right

my @stuff = 0 .. 9; my @lastitem ; for my $item ( @stuff ){ my $next = $item; if( @lastitem and $item eq $lastitem[-1] ){ print "they measure up\n"; } push @lastitem, $item; shift @lastitem while 3 == @lastitem; ### keep at most 3 last items }

Replies are listed 'Best First'.
Re^4: Editing Listed Data (index, stack)
by Anonymous Monk on Mar 25, 2013 at 01:06 UTC

    And I didn't preview enough :) or test the logic   shift @lastitem while  @lastitem > 3; ### keep at most 3 last items

      Thanks! That was just the sort of stimulation I needed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025211]
[choroba]: Also, I'd try starting the App with the US layout and switch to the dead keys layout later
[Tux]: no, it was not .xinput related, but that did ring a bell. digging in my $ENV I found the cause: XMODIFIERS=@im=xim
[Tux]: Héŀłő çřűèļ ŵøŗľđ, ıŧ'š ºvəř

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2017-09-25 20:54 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (290 votes). Check out past polls.