Regarding your update, where you show a modified version of the code I suggested, I should point out a minor (harmless) misconception in your final "for" loop to print the results. Here's your version of the for loop:
for (sort keys %data) { my @content = "$data{$_},$_"; if (grep(/.*,.*,.*,.*$/, @content)){ @column = grep(/.*,.*,.*,.*$/, @content) }; #@column = grep(/.*,.*,.*,.*$/, @content); print $fh_out "@column\n"; }
The second line assigns a single string to an array (@content). This is bad form, but perl will "do the right thing" and create the array with a single element, which is the string you supplied: "$data{$_},$_" .

Next, the "if" condition returns true if the single array element happens to match your regex, where you appear to be testing whether a string contains at least three commas. If it matches, you use grep on the single element in @content to store the string in a new array "@column". (Looks like you have removed "use strict;" because @column appears to be working as an undeclared global -- bad idea.) Anyway, if the match fails, @column remains undefined. UPDATE: rather, @column remains unchanged -- it still holds whatever may have been assigned to it previously, if anything. That could cause a problem in your output.

Regardless of what happens with that if condition, you print @column followed by a line-feed (even if it's undefined --update: or contains a string from a previous iteration).

All of that would be better stated as follows:

for ( sort keys %data ) { print "$data{$_},$_\n" if ( $data{$_} =~ /,.*?,/ ); }
(that is, if the original value of "$data{$_}" contains two commas, add ",$_\n" at the end and print it; otherwise skip it)

In reply to Re: Joining two files on common field by graff
in thread Joining two files on common field by ch1

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.