I'm building on the knowledge shared in a previous question: What native Perl code replaces `cut`?
A Perl script uses this code:
my $cmd = "cat $TMPDIR/files.* | sort | uniq > $File"
`$cmd`
I'm trying to rebuild the above function with native Perl to run on MS Windows. I have this so far, but it's not quite working:
my $globPat = "$TMPDIR/parts.*"
my $outFile = "$TMPDIR/out.txt"
my %lines;
# 1) glob all files
while (my $glob = glob($globPat)) {
open(IN, "<", "$glob") or die("Can't read $glob");
# collect lines as unique keys in a hash
++$lines{ ($_)[1] } while <IN>;
close(IN);
}
# sort the key and save values to $glueFile
open(OUT, ">", "$outFile") or die("ERROR: Can't write $outFile");
foreach my $key (sort keys %lines) {
print OUT $lines{$key} . "\n";
}
close(OUT)
I'm getting a variety of errors that bounce around (line numbers) as I try to troubleshoot. Can someone help sort out 1) how to properly use glob, 2) how to add lines read from the various files to one hash key and 3) sort the hash's keys (lines) and print them to the new output file.
($_)[1]
.... ? Why would that work when you loop over a list of scalar values? – TLP