Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

extracting dissimilar lines

by anasuya (Novice)
on Mar 26, 2012 at 09:21 UTC ( #961627=perlquestion: print w/ replies, xml ) Need Help??
anasuya has asked for the wisdom of the Perl Monks concerning the following question:

Hi. This is in connection with a similar query which I had posted yesterday. I have a file which looks like this.

2gqt_FAD_A_601 = d.145.1.2- 2i0z_FAD_A_501 = c.3.1.8-e.74.1.1- 1n1e_NDE_A_400 = a.100.1.6-c.2.1.6- 2qzl_IXS_A_449 = b.50.1.2- 2ydx_TXP_E1339 = c.2.1.2- 3g8i_RO7_A___1 = c.2.1.2- 1llf_F23_A_800 = c.69.1.17- 1y0g_8PP_A_320 = b.61.6.1- 1ewf_PC1_A_577 = d.83.1.1- 2a94_AP0_A_336 = c.2.1.5-d.162.1.1- 1fm9_570_D_200 = a.123.1.1- 2zmh_NYA_A___1 = a.123.1.1-

I need to extract out lines, which have dissimilar values after the '=' sign. The output should look something like this:

2gqt_FAD_A_601 = d.145.1.2- 2i0z_FAD_A_501 = c.3.1.8-e.74.1.1- 1n1e_NDE_A_400 = a.100.1.6-c.2.1.6- 2qzl_IXS_A_449 = b.50.1.2- 1llf_F23_A_800 = c.69.1.17- 1y0g_8PP_A_320 = b.61.6.1- 1ewf_PC1_A_577 = d.83.1.1- 2a94_AP0_A_336 = c.2.1.5-d.162.1.1-

How do I do this? Please help.

Comment on extracting dissimilar lines
Select or Download Code
Re: extracting dissimilar lines
by JavaFan (Canon) on Mar 26, 2012 at 09:28 UTC
    Untested:
    $/ = ""; while (<>) { my ($l1, $l2) = split /\n/; $l1 =~ /=/p; my $a1 = ${^POSTMATCH}; $l2 =~ /=/p; my $a2 = ${^POSTMATCH}; next if $a1 eq $a2; print; }
      A bit shorter, using no split, no additional variables and only one regex:
      $/ = ""; while (<>) { /=\s*([^\n]+).*=\s*([^\n]+)/s; print unless $1 eq $2; }

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics
        That would fail if the second line contains more than one '='. But, if you think there's value in replacing trivial regexes (which can be dealt with by the optimizer) with complicated ones, why not:
        $/ = ""; /=(\N+)\n[^=]+=\1\n/ or print while <>;
        Or, as a one-liner:
        perl -00ne'/=(\N+)\n[^=]+=\1\n/ or print' data-file
Re: extracting dissimilar lines
by stevieb (Hermit) on Mar 26, 2012 at 13:38 UTC

    You could *very* easily do this check within this section of the code I wrote for you yesterday in this post

    if ( scalar @hold > 1 ){ push @data, [ @hold ]; }

    What have you tried?

Re: extracting dissimilar lines
by trizen (Friar) on Mar 26, 2012 at 18:28 UTC
    use 5.010; while (<>) { my $p = /=/p ? ${^POSTMATCH} : next; next if (my $c = <>) =~ /=/p and $p eq ${^POSTMATCH}; say $_, $c; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-09-19 11:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (135 votes), past polls