Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Editing Listed Data

by jtucker (Initiate)
on Mar 25, 2013 at 00:30 UTC ( #1025203=perlquestion: print w/ replies, xml ) Need Help??
jtucker has asked for the wisdom of the Perl Monks concerning the following question:

Monks:

I am bit stuck on this one.

I have a data set such as this:

1:1 word1
1:1 word2
1:1 word3
1:1 word4
1:2 word1
1:2 word2
1:2 word3
1:3 word1
1:3 word2
1:3 word3

I have stored each line into an array. I need to distinguish where the increment occurs and insert a header, thus transforming my data to:

>1:1
1:1 word1
1:1 word2
1:1 word3
1:1 word4
>1:2
1:2 word1
1:2 word2
1:2 word3
>1:3
1:3 word1
1:3 word2
1:3 word3

Any insights would be greatly appreciated.

Comment on Editing Listed Data
Re: Editing Listed Data
by igelkott (Curate) on Mar 25, 2013 at 00:38 UTC

    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
      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 }
Re: Editing Listed Data
by kcott (Abbot) on Mar 25, 2013 at 04:34 UTC

    G'day jtucker,

    Welcome to the monastery.

    As you haven't shown any code, it's difficult to know where you're getting stuck with this. For instance, your earlier comment "What I an hung on is the third bullet." [sic] is meaningless to me: it's the first and only time you've mentioned bullets; there are no bulleted lists; how does the 3rd bullet (not shown) differ from the 1st and 2nd bullets (also not shown). In order to get a better answer when you post here, you'd do well to read these guidelines: How do I post a question effectively?

    The following skeleton code may help resolve whatever problem you're experiencing. If not, please post any follow-up questions in line with the guidelines I've linked to above.

    $ perl -Mstrict -Mwarnings -E ' my @data = ( q{1:1 word1}, q{1:1 word2}, q{1:1 word3}, q{1:1 word4}, q{1:2 word1}, q{1:2 word2}, q{1:2 word3}, q{1:3 word1}, q{1:3 word2}, q{1:3 word3} ); my $old_head = q{}; for (@data) { my $new_head = (split)[0]; if ($new_head ne $old_head) { say "> $new_head"; $old_head = $new_head; } say; } ' > 1:1 1:1 word1 1:1 word2 1:1 word3 1:1 word4 > 1:2 1:2 word1 1:2 word2 1:2 word3 > 1:3 1:3 word1 1:3 word2 1:3 word3

    -- Ken

      To clarify, my comment regarding the bullets was in conjunction to another's response. It probably would have been best to read the comment as a response, since it appears, at least on my display, indented to the aforesaid comment.

      I appreciate the response. I am aware of the forum guidelines. But since my question was a very limited subset of data, namely an array, I was just needing a prompt in how to compare a scalar item to the previous item on the stack. The fence post error served as a necessary reminder to debug, as mentioned by the previous comments.

      So my code was
      @line = split /\r/m, $file; for my $line (@line) { my $header = ((split(/\t/, $line))[0]); ##stuck here as I was unsure how to compare $header to previously seen + header }
      So, thanks for your response too. It showed me another way to resolve the issue. Much appreciated.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-09-17 21:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (100 votes), past polls