Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: find common data in multiple files

by Anonymous Monk
on Dec 28, 2017 at 13:19 UTC ( #1206320=note: print w/replies, xml ) Need Help??


in reply to find common data in multiple files

Don't do this:
$result{$key} = 1; $result{$key}++;
Instead, omit the first line. If a hash-key does not yet exist when you "increment" it, it will automatically be created with the value zero, then incremented to the value 1. (In the code as written, the value can never be anything but 1.)

The remainder of the solution should be straightforward: after processing all 25 files, look for keys with the value 25. (Assuming that you know that there are no duplicates in each file.) If there could be duplicates, you will need to use a second hash, cleared at the start of each file, and use the exists verb to see if a specified key is already in it (i.e. is a duplicate within this file).

Replies are listed 'Best First'.
Re^2: find common data in multiple files
by 1nickt (Monsignor) on Dec 28, 2017 at 13:46 UTC

    Don't do this:
    $result{$key} = 1; $result{$key}++;
    ... (In the code as written, the value can never be anything but 1.)

    Sorry, that's wrong.

    $ perl -E '$key = "wrong"; $result{$key} = 1; $result{$key}++; say val +ues %result'
    2


    The way forward always starts with a minimal test.
      "So, sue me ..." ... two!
      Nevertheless, not the increment-behavior that the programmer intended, which was the essential point. The value will always be one two.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2018-08-19 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (186 votes). Check out past polls.

    Notices?