Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: comparing an ID fom one file to the records in the second file

by sundialsvc4 (Abbot)
on Dec 01, 2017 at 17:59 UTC ( #1204693=note: print w/replies, xml ) Need Help??


in reply to comparing an ID fom one file to the records in the second file

I think that in my approach to this problem I would “flip” it:   let the second file drive the show, not the first!

Since your first file appears to contain nothing other than a list of “keys of interest,” let’s start the show by a simple loop that adds each key to a hash with a do-nothing value.   (Our only future concern will be whether-or-not the key exists.)   Something like:

my $keys = {}; while (<FILEHANDLE>) {chomp; $keys[$_] = 1;}

Having thus iterated one time through this file, in order to assemble a list of the keys that it contains, we are finished with it.   (These are not “nested” loops.)

Now, we loop through the second file, line-by-line, perhaps split() it into an array of its components, and then check for exists($keys, $recordArray[2]).   If the previously-assembled hash contains the specified key, then this record must be a record of interest.

  • Comment on Re: comparing an ID fom one file to the records in the second file
  • Download Code

Replies are listed 'Best First'.
Re^2: comparing an ID fom one file to the records in the second file
by stevieb (Abbot) on Dec 01, 2017 at 18:30 UTC

    You said:

    my $keys = {}; while (<FILEHANDLE>) {chomp; $keys[$_] = 1;}

    $keys is not an array, you've defined it as a hash reference. You meant $keys->{$_} ....

    Then you said:

    exists($keys, $recordArray[2])

    exists() does not work like that.

    exists $keys->{$recordArray[2]};

    Please, if you don't test the code you supply, acknowledge that in writing.

Re^2: comparing an ID fom one file to the records in the second file
by Laurent_R (Canon) on Dec 02, 2017 at 05:02 UTC
    Hi sundialsvc4,

    I think that in my approach to this problem I would “flip” it: let the second file drive the show, not the first!

    Since your first file appears to contain nothing other than a list of “keys of interest,” let’s start the show by a simple loop that adds each key to a hash with a do-nothing value.

    Just in case you didn't notice, that's exactly the approach suggested earlier by both poj and myself. I even explicitly explained that there were two possible approaches and that, in this case, it was probably a bit better to store the keys taken from the first file in the hash. Although, to tell the truth, choosing between the two approaches would require to know more about the respective size of the files. If the second file is much shorter than the first one, it might be better to choose the other way around.

Re^2: comparing an ID fom one file to the records in the second file
by karlgoethebier (Monsignor) on Dec 03, 2017 at 06:13 UTC
    "...my approach to this problem..."
    "One Step Forward, Two Steps Back" (Vladimir Ilyich Lenin)

    See also

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2019-07-20 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?