Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Sorting text files.

by Laurent_R (Canon)
on Nov 06, 2015 at 20:07 UTC ( [id://1147133]=note: print w/replies, xml ) Need Help??


in reply to Sorting text files.

If you can fit the first file into a hash (key: first field, value: full line), then you don't even need to use the sort function: load file 1 into the hash, read file 2 and, for each line of file 2, lookup into the hash with the key, print the line from the hash, delete the hash record; at the end, print out the hash records still there.

Replies are listed 'Best First'.
Re^2: Sorting text files.
by Anonymous Monk on Nov 06, 2015 at 21:04 UTC

    Yeah, why sort when you don't have to :)

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1147112 use Inline::Files; use strict; use warnings; my %id; $id{ s/;.*//sr } .= $_ while <FILE1>; print delete @id{ map s/\n//r, <FILE2> }, values %id; __FILE1__ 1 HD;BSkyB:11097:VC23M5O25P0S1:S28.2E:23000:2305=27:2307=NAR@4;2306=en +g@106:2308;2309=eng:0:21000:2:2066:0 2 HD;BSkyB:11097:VC23M5O25P0S1:S28.2E:23000:2315=27:2316=NAR@4;2317=en +g@106:2318;2319=eng:0:21020:2:2066:0 3 HD;BSkyB:11097:VC23M5O25P0S1:S28.2E:23000:2320=27:2321=NAR@4;2322=en +g@106:2323;2324=eng:0:21030:2:2066:0 ITV HD;BSkyB:11097:VC23M5O25P0S1:S28.2E:23000:2305=27:2307=NAR@4;2306= +eng@106:2308;2309=eng:0:21000:2:2066:0 __FILE2__ 3 HD 1 HD 2 HD

      What happens if there are duplicate keys?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.

        That's why the .= is there :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-20 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found