Just a few "peripheral" comments about your code:
chomp($phi = `ls slk`); chomp($phi2 = `ls *.csv`); open(OUT,"> /home/Aug18.csv") or die "Cannot open output file; open(READDATA, "< /home/$phi") or die "Cannot Open file;
Since you know you are going to open a file named "slk", why not just open( READDATA, "slk" ) or die $!; -- no need to run a "ls" in a sub-shell to get the file name!

And the second "chomp" line doesn't do what you think (neither would the first one, if "slk" happened to be a directory); if the "ls *.csv" is likely to return more than one file, then you want to assign its output to an array:

@phi2 = `ls *.csv`; chomp @phi2; # chomp will apply to every element of the array
(Of course, if there's only one *.csv file, then your usage as shown in the OP would work, just by coincidence.)

In general, if you have just two files that the script is supposed to deal with, it makes more sense to have the two file names provided as command-line arguments (available to the script as @ARGV). The script can print to STDOUT, and you can redirect that on the command line to some other file. So the command line would look like this:

your_script.pl slk someinput.csv > /home/Aug18.csv
And the script would look like this (I haven't tested it, since I don't have appropriate data, but it does compile):
#!/usr/bin/perl -w use strict; die "Usage: $0 infile1 infile2\n" unless (@ARGV==2 and -f $ARGV[0]); my ($infile1, $infile2) = @ARGV; open( READDATA, $infile1 ) or die "Cannot open $infile1: $!\n"; my %h = (); while (<READDATA>) { if ( /(\d{4}\w\d?)\s+(\w*)/ ){ my ( $port, $lsn ) = ( $1, $2 ); $h{$port} = $lsn; } } close READDATA; open( READDATA2, $infile2 ) or die "Cannot open $infile2: $!\n"; while (<READDATA2>) { chomp; if ( /(\d{4}\w\d?)/ and exists( $h{$1} )) { print "$_,$h{$1}\n"; } }
Note that while tcf22's reply got to the core issue (just look for the existence of a matching hash key -- don't loop over the entire hash -- when reading each line of the second file), tcf22's suggested code might not do the same thing as your original code. (And tcf22's suggestion kept your strange handling of file names, which makes me wonder...)

You didn't say exactly what was in the second file that you are reading, and tcf22 assumed that each line of that file contained just a string that would match a "port" string. But according to you original code, if the first file had a port string like "1234x", then you should get a match when the second file has a line like "foo bar 1234x baz".

For that matter, if file1 mentions two ports, "1234x" and "1234x5", and file2 contains a line with just "1234x", then your original code (looping over all the hash keys) would print two matches -- so in that sense, the code I'm suggesting above doesn't really match your original script's behavior either. (But I think mine works the way you intended -- using the same regex when reading both files, and then using $1 as the hash key, will always yield just the single exact match. (Now, you just need to worry about whether one or the other file happens to contain multiple lines with the same port pattern.)

Finally, please do note how my example differs from yours in terms of indentation -- this is important for legibility. And using $_ is not a bad thing, it is a Good Thing. (I don't know why we've been seeing such a slew of recent SoPW posts that seem unwilling to use $_.)


In reply to Re: 2 files 1 output by graff
in thread 2 files 1 output by Anonymous Monk

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.