Dear Monks, I just started with perl and seek your advice. I have one large pipe '|' separated text file with many columns. I have another small text file with only one column. I need to write out or extract only those records (lines) from the large file, to a new file, if the value in column 1 matches with the value in column 1 of the smaller file. Here is my code and I am sure it is faulty and so seek your advice :) it is actually returning all lines from File1.txt. But what I need is only those lines where column 1 data is matching. Thanks in advance.

#!c:\perl\bin\perl # Set file paths $FILE1 = "D\:\\Data\\WES\\File1.txt"; $FILE2 = "D\:\\Data\\WES\\File2.txt"; $FILE3 = "D\:\\Data\\WES\\File3.txt"; # print "$FILE1\n"; # print "$FILE2\n"; # print "$FILE3\n"; my %F1hash; open(F1,'<', $FILE1) or die "Can't open $FILE1\n"; open(F2,'<', $FILE2) or die "Can't open $FILE2\n"; open(F3,'>', $FILE3) or die "Can't open $FILE3\n"; print F3 "\n"; # print LOGFILE "$start_date \n"; print F3 "===========================================\n"; print F3 "The following Partnumbers have been synchronized\n"; print F3 "===========================================\n"; print F3 "\n"; my %F1hash = (); while (<F2>) { $ptfkey = $_; $ptfpn = $ptfkey; $ptfpn =~ s/^\s+|\s+$//g; $F1hash{$ptfkey, $ptfpn} = $ptfpn; } close F2; while (<F1>) { #chomp; $uline = $_; @ufields = split(/\|/, $uline); # print "$ufields"; $PartNumber = $ufields[0]; $Std_Cost = $ufields[1]; $Last_Paid_Price = $ufields[2]; $Qty_In_Stock = $ufields[3]; $Moto_Preferred_Part = $ufields[7]; $Rev = $ufields[9]; $Agile_Description = $ufields[10]; if ($PartNumber =~ $Flhash{$PartNumber}) { #print "$ufields[0] \n"; print F3 "$PartNumber|$Std_Cost|$Last_Paid_Price|$Qty_ +In_Stock|$Moto_Preferred_Part|$Rev|$Agile_Description\n"; } } close F1; close F3; exit;

In reply to Question on file compare by sureshsmr

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.