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.

use strict; use warnings; my $file2 =<<EOF2; Code: description 1: Generic user error 2: Error with file location 3: File not found 4: Syntax error EOF2 my @desc = split m{\n+}, $file2; my %desc; for my $line (@desc[1 .. $#desc]) # Skip headings { chomp $line; my ($code, $desc) = $line =~ m{ ^ (\d+) : \s* (.+) $ }x; $desc{$code} = $desc; } <DATA>; # Skip headings while (my $line = <DATA>) { chomp $line; my @fields = split m{,\s*}, $line; if (exists $desc{$fields[1]}) { print join(', ', @fields, $desc{$fields[1]}), "\n"; } } __DATA__ Testname, code, date, file Test1, 4, 4/11/15, /tmp Test2, 2, 4/11/15, /log Test3, 1, 4/11/15, /log Test4, 7, 4/11/15, /dat Test5, 4, 4/11/15, /log

Output:

14:45 >perl 1214_SoPW.pl Test1, 4, 4/11/15, /tmp, Syntax error Test2, 2, 4/11/15, /log, Error with file location Test3, 1, 4/11/15, /log, Generic user error Test5, 4, 4/11/15, /log, Syntax error 14:45 >

Hope that helps,

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


In reply to Re: Perl logic request for an IO scenario by Athanasius
in thread perl logic request for an IO scenario by Apronline

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.