Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2018-02-23 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (307 votes). Check out past polls.

    Notices?