Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^7: compare lines within a file

by garyboyd (Acolyte)
on Mar 15, 2011 at 10:19 UTC ( [id://893280]=note: print w/replies, xml ) Need Help??


in reply to Re^6: compare lines within a file
in thread compare lines within a file

Thanks kennethk, this works, but I cannot figure out how to incorporate this into a program that can take a list and then check each line of the list to see if subsequent entries compare and if they do to then print out.

It is possible to do this if the name is specified in the code

if ($row->[0] =~ m{\QHWUSI-EAS95L_0025_FC:3:1:5232:1082#0/\E}) {

but how to do this if you do not know what each line is??

I tried using the following regex, which will match each line:

         if ($header2 =~  m{HWUSI-EAS95L_0025_FC:3:1:\d*:\d*#0/}) {

but not sure how to take it from there.....

Replies are listed 'Best First'.
Re^8: compare lines within a file
by kennethk (Abbot) on Mar 15, 2011 at 14:52 UTC
    My general approach in this type of situation is to build a hash, or more specifically a hash of lists. I am not familiar with your particular input, and figuring out the patterns and how to parse them is usually where most of the effort in writing a script of this type falls. Taking the input from your original post:

    HWUSI-EAS95L_0025_FC:3:1:5232:1082#0/1 - 1449586 1449619 HWUSI-EAS95L_0025_FC:3:1:5232:1082#0/2 - 1449544 1449577 HWUSI-EAS95L_0025_FC:3:1:6417:1078#0/1 - 4744083 4744113 HWUSI-EAS95L_0025_FC:3:1:6539:1083#0/1 - 4867122 4867157 HWUSI-EAS95L_0025_FC:3:1:6539:1083#0/2 - 4866942 4866977 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0/1 + 1930232 1930266 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0/2 + 1930354 1930389

    And fitting the most general pattern that seems to match, I would use code like

    #!/usr/bin/perl use strict; use warnings; use Text::CSV; my @result; my $csv = Text::CSV->new ( { sep_char => "\t" } ) # should set binary + attribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); my %result; while ( my $row = $csv->getline( *DATA ) ) { my ($key, $index) = $row->[0] =~ m{^(.+)/([12])$} or die "Line did not match pattern: @$row"; if ($index == 1) { $result{$key}[0] = $row->[2]; } elsif ($index == 2) { $result{$key}[1] = $row->[2]; } else { die "Index was not 1 or 2: @$row" } } $csv->eof or $csv->error_diag(); # Output results: for my $key (keys %result) { next unless $result{$key}[1]; print "$key:\t$result{$key}[0]\t$result{$key}[1]\n"; } __DATA__ HWUSI-EAS95L_0025_FC:3:1:5232:1082#0/1 - 1449586 1449619 HWUSI-EAS95L_0025_FC:3:1:5232:1082#0/2 - 1449544 1449577 HWUSI-EAS95L_0025_FC:3:1:6417:1078#0/1 - 4744083 4744113 HWUSI-EAS95L_0025_FC:3:1:6539:1083#0/1 - 4867122 4867157 HWUSI-EAS95L_0025_FC:3:1:6539:1083#0/2 - 4866942 4866977 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0/1 + 1930232 1930266 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0/2 + 1930354 1930389
    to get the output

    HWUSI-EAS95L_0025_FC:3:1:6539:1083#0: 4867122 4866942 HWUSI-EAS95L_0025_FC:3:1:5232:1082#0: 1449586 1449544 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0: 1930232 1930354

    Note that because of how tabs get mangled on this site, you'll need to click the download link in order to get the proper formatting in your clipboard.

      brilliant! thank you so much for your help kennethk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-25 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found