Bill said:
The objective is to create a sum of the two files with out any duplications.
I included "use" into the script, but all it does now is make the file copy
and loops with out making any comapisons with output6.txt.
Here's the problem: we're not psychic. You asked for a solution that
copied one file to another, and you got that. Now you appear to want
to roll your own version of diff(1), instead. We can't help you
unless we know what you actually want-- if you ask for something else,
don't be surprised if you get that instead.
# To use this script, type the following at the command line prompt:
# bbb.pl file1 file2 output_file_name
open (INPUT1, "$ARGV[0]") or die "Cannot open $ARGV[0]";
Useless use of quotes, and you didn't use $! in your error string:
open (INPUT1, $ARGV[0]) or die "Cannot open $ARGV[0]: $!";
Also, every Perl program you write should begin with:
#!/path/to/perl # please put the REAL path to perl here!!!
use warnings;
use strict;
open (OUTPUT, "> $ARGV[2]");
Why didn't you die() on error here, too?
open (OUTPUT, "> $ARGV[2]") or die "Cannot open $ARGV[2]: $!";
Only that's wrong, you don't want to do that here. Wait until after
you've done the copy, and THEN open it for append, not write.
#print "First input file is: $ARGV[0]\n";
#print "Second input file is: $ARGV[1]\n";
#print "The output file is: $ARGV[2]\n\n";
# This half of the code dumps everything in 'file1' to the output 'file3'
#while ( <INPUT1> ) {
# print "Reading $ARGV[0]\n";
# print OUTPUT $_;
#}
use File::Copy;
copy("$ARGV[0]","$ARGV[2]") or die "Copy failed: $!";
copy($ARGV[0], $ARGV[2]) or die "Copy failed: $!";
There's no need to quote variables by themselves like that. It can
even cause problems in some cases. Stop doing that.
Also, everything that happened before happens now-- i.e., after the
copy() call is through, file3 is an exact copy of file1.
# This half of the code compares 'file2' to 'file1' and writes out
# any line that doesn't match to the output 'file3'
open (INPUT1, "$ARGV[1]") or die "Cannot open $ARGV[1]";
open(INPUT1, $ARGV[1] or die "Cannot open $ARGV[1]: $!";
Why are you re-opening INPUT1 here? You're using INPUT2 below, but
you never open it. Are you meaning to do that here?
You also want to open $ARGV[2] for output here:
open(OUTPUT, '>>', $ARGV[2]) or die "couldn't open $ARGV[2] for append: $!";
while ( <INPUT1> ) {
$match = 0;
$a = $_;
open (INPUT2, "$ARGV[0]") or die "Cannot open $ARGV[0]";
open (INPUT2, $ARGV[0]) or die "Cannot open $ARGV[0]: $!";
while ( <INPUT2> ) {
$b = $_;
if ($a eq $b) {
$match = 1;
last;
}
}
if ($match == 0) {
print OUTPUT $a;
}
Ick. Try this (untested)
print OUTPUT $a unless grep /^\Q$a\E$/ said:
This is a mess, and I'm fairly sure none of it does either what you
think it does, or what you want it to. First, you read a line from
the second input file. Then you assign it to $a. Remember, INPUT1 is
a filehandle for the contents of $ARGV[1].
Then you read from INPUT2, which you never opened, so naturally
nothing is read. If you'd enabled warnings, perl would have said
something like: "readline() on unopened filehandle INPUT2 at - line
<foo>." Anyway, the logic is completely reversed-- after the copy,
file3 is the same as file1, so you should isntead read file2, and then
print out whatever's in that file that is NOT in file1.
First off, this is a generally horrible solution, as you have to read
through the entire contents of file1 EACH BLOODY TIME, which means
horrid runtimes. A much better solution is to read both files, once,
into a hash, and let that handle uniqueness for you:
#!/usr/bin/perl
use warnings;
use strict;
my $output = pop @ARGV;
my %lines = ();
while(<>) {
chomp;
$lines{$_}++;
}
open(OUTPUT, '>', $output) or die "couldn't open $output: $!";
print OUTPUT join("\n", keys %lines), "\n";
There's probably even better solutions; this is just off the top of my
head.
-=Eric