Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Parsing problem

by bluedevil (Initiate)
on Jul 29, 2009 at 04:22 UTC ( [id://784124]=perlquestion: print w/replies, xml ) Need Help??

bluedevil has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, How can I get output like this ?
1 2 - perlmonks1 sdweq23 3 4 - javaforums2 sd341 5 6 - deamtech3 fr4frf34 7 8 + dreamtech3 dcvdf3443 9 10 + sunforum4 sdcds3443 11 12 - sunforum4 ddfvdv333
output like this:-
1 2 - perlmonks1 sdweq23 one 3 4 - javaforums2 sd341 one 5 6 - deamtech3 fr4frf34 start 7 8 + dreamtech3 dcvdf3443 end 9 10 + sunforum4 sdcds3443 start 11 12 - sunforum4 ddfvdv333 end
I want to find out the smallest and largest value in field 1 and then print it as start / end and if there is only 1 key value pair then print it as one. Thanks

Replies are listed 'Best First'.
Re: Parsing problem
by GrandFather (Saint) on Jul 29, 2009 at 04:42 UTC
    use strict; use warnings; print <<BLOCK1; 1 2 - perlmonks1 sdweq23 3 4 - javaforums2 sd341 5 6 - deamtech3 fr4frf34 7 8 + dreamtech3 dcvdf3443 9 10 + sunforum4 sdcds3443 11 12 - sunforum4 ddfvdv333 BLOCK1 print <<BLOCK2; 1 2 - perlmonks1 sdweq23 one 3 4 - javaforums2 sd341 one 5 6 - deamtech3 fr4frf34 start 7 8 + dreamtech3 dcvdf3443 end 9 10 + sunforum4 sdcds3443 start 11 12 - sunforum4 ddfvdv333 end BLOCK2

    or was there more you wanted to tell us about your problem?


    True laziness is hard work
      Lol :)
      Good reply grandfather :)
      However, I think i means he wants to find out some kind of session end/beginning for the users
      Well that's an "assumption" only :)
      TIMTOWTDI
      Put in a hash or AoA, check for duplicity, flag it. Print out the hash/aoa
      Raghu
      I want to find out the smallest and largest value in field 1 and then print it as start / end and if there is only 1 key value pair then print it as one.
        Not sure what you consider to be the key/value pair

        This code should get you started.

        use strict; use warnings; my @list = split /\n/, <<BLOCK1; 1 2 - perlmonks1 sdweq23 3 4 - javaforums2 sd341 5 6 - dreamtech3 fr4frf34 6.5 6.6 - dreamtech3 fr4frf34 7 8 + dreamtech3 dcvdf3443 9 10 + sunforum4 sdcds3443 11 12 - sunforum4 ddfvdv333 BLOCK1 my ($oldname1,$oldname2) =("") x 2; my ($buf, $occurrances)=(0,0); my ($max,$min); for (@list,""){ my ($num1,$num2,$sign,$name1, $name2) = split; if ($name1 && $name1 eq $oldname1){ $occurrances++ ==1 and $buf .= " start"; }else{ if ($occurrances > 1){ $buf .= " end"; }elsif ($occurrances ==1){ $buf .= " one"; } $occurrances = 1; } (! defined $max) || ($num1 && $num1 > $max ) and $max = $num1; (! defined $min) || ($num1 && $num1 < $min) and $min=$num1 ; print "$buf\n" if $buf; $buf = $_; $oldname1=$name1; } print "Min=$min; Max=$max\n";

             Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Re: Parsing problem
by bichonfrise74 (Vicar) on Jul 30, 2009 at 03:44 UTC
    Here's a little hack that might get you on track. I had to read the input twice so that I can process it. I'm sure there is better way to do this.
    #!/usr/bin/perl use strict; my $data_pos = tell DATA; my %record_of; while (my $line = <DATA>) { chomp( $line ); my ($key, $val) = (split /\s+/, $line)[3,4]; $record_of{$key}++; } seek DATA, $data_pos, 0; while (my $line = <DATA>) { chomp( $line ); my ($key, $val) = (split /\s+/, $line)[3,4]; print "$line one\n" if ( $record_of{$key} == 1 ); print "$line end\n" if ( $record_of{$key} == 0 ); if ( $record_of{$key} == 2 ) { $record_of{$key} = 0; print "$line start\n"; next; } } __DATA__ 1 2 - perlmonks1 sdweq23 3 4 - javaforums2 sd341 5 6 - dreamtech3 fr4frf34 7 8 + dreamtech3 dcvdf3443 9 10 + sunforum4 sdcds3443 11 12 - sunforum4 ddfvdv333

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-23 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found