Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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.

Replies are listed 'Best First'.
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 (Monsignor) 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 (Hermit) 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?
[Corion]: I guess I should not read some discussions on HackerNews - the crowd there is weirdly different - "you have to stay current in tech" (by following all the latest JS trends) is a meme there, but it doesn't really match my experience
[Corion]: At least in my area, experience can beat most new tech anyway because most new tech is just a rehash of things abandoned in the 70s and we still have a lot of that ;)
[Corion]: choroba: Ah, that will be next Friday for $work ;) Drinking enough water is key ;))
[Corion]: But then, maybe that's just my experience with things, and maybe Hacker News is just people much younger who haven't seen more than one tech cycle...
[choroba]: s/water/bear/ and s/enough/too much/
[choroba]: beer
[choroba]: oh ok
[marto]: I crashed the ScotLUG Christmas night, having never actually been to ScotLUG
[choroba]: Corion yeah, I probably already told you about how Bjarne Stroustrup was asked whether he still watched the new tech trends and what really impressed him
[choroba]: His reply was "I watch them, but I haven't been impressed in the last 10 years. There's been nothing new". That was 2 years ago :)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2016-12-09 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (150 votes). Check out past polls.