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

Re: Editing Listed Data

by igelkott (Curate)
on Mar 25, 2013 at 00:38 UTC ( #1025205=note: print w/ replies, xml ) Need Help??


in reply to Editing Listed Data

Roughly:

  • loop through array
  • split array element on white-space
  • check if first part of this one matches the previous
  • print header if it doesn't
  • store first part for next round
  • end loop


Comment on Re: Editing Listed Data
Re^2: Editing Listed Data
by jtucker (Initiate) on Mar 25, 2013 at 00:47 UTC
    Yes, I am aware of the logic of my question. What I an hung on is the third bullet. How Do I iterate through the array, yet compare the next item against the former?

      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 }

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025205]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-12-26 09:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls