in reply to 2 files 1 output
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!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;
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:
(Of course, if there's only one *.csv file, then your usage as shown in the OP would work, just by coincidence.)@phi2 = `ls *.csv`; chomp @phi2; # chomp will apply to every element of the array
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:
And the script would look like this (I haven't tested it, since I don't have appropriate data, but it does compile):your_script.pl slk someinput.csv > /home/Aug18.csv
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...)#!/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"; } }
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 $_.)
|
|---|