Apronline has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I need help in building a logic for below scenario. I am using PERL to read 2 files which have 4 and 2 elements as below. I need to read each line from File1 and compare if “code” exists in both files. If code exists in both files, I need to display all the line elements of file1 along with its “description”. Can you please share some thoughts? Thanks in advance. File1:
Testname, code, date, file Test1, 4, 4/11/15, /tmp Test2, 2, 4/11/15, /log Test3, 1, 4/11/15, /log
File2:
Code: description 1: Generic user error 2: Error with file location 3: File not found 4: Syntax error

Replies are listed 'Best First'.
Re: Perl logic request for an IO scenario
by Athanasius (Archbishop) on Apr 12, 2015 at 04:46 UTC

    Hello Apronline,

    In Perl, the usual way to handle this sort of lookup task is with a hash. As the hash data resides in memory, the hash should be populated with data from the smaller of the two files. In this case, it appears that File2 is the shorter, so you should begin by building a hash from File2 with key/value pairs of the form: 1 => 'Generic user error'.

    Once you have this hash (i.e., lookup table), it is a straightforward task to go through the data in File1, line by line, extract the code element, and look it up in the hash using the exists function.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: perl logic request for an IO scenario
by hippo (Archbishop) on Apr 12, 2015 at 10:32 UTC

    The program to do this already exists: join. There is a similar program written in Perl which might also suit your needs.