Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: make a search pattern and remove duplicate from the file

by jimpudar (Pilgrim)
on May 11, 2018 at 16:15 UTC ( [id://1214390]=note: print w/replies, xml ) Need Help??


in reply to Re: make a search pattern and remove duplicate from the file
in thread make a search pattern and remove duplicate from the file

I think what he is asking for is to print out only the first instance of any line which has a unique value of the pet, hate tuple.

Any other lines should be ignored.

Since the structure you are building does not keep track of which line came first, looping over it will never bring you to the answer he is looking for.

If you really wanted to do this with a one liner, (and I'm definitely not saying you should), I would do this, which will print exactly the result he asked for:

perl -wlF'\|' -e 'for (@F) { /(\w+)="(\w+)"/; $r{$1} = $2 } $x="$r{pet}:$r{hate}"; $s{$x} ? next : ++$s{$x} && print' <input pet="cat"|hate="rat"|like="dog" pet="cow"|hate="rat"|like="dog"

Best,

Jim

Replies are listed 'Best First'.
Re^3: make a search pattern and remove duplicate from the file
by NetWallah (Canon) on May 11, 2018 at 20:15 UTC
    Your comment propagates the OP's usage of the word "unique" - and that causes confusion because your implementation reports the "first" usage of the tuple, which may not necessarily be a "unique" occurrence.

    In order to find "unique", you will need to do that after all records have been ingested, and post-process, as my code does.

    Anyway - this only illustrates non-specific specifications - and these nits are not worth picking.

    Cheers.

                    Memory fault   --   brain fried

      Very good point, I didn't really think about the meaning of "unique" but just tried to make sense of the desired output.

      Best,

      Jim

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found