Hi everyone,
I'm working on a script that takes in a certain file A and compares it against several other files and prints any lines that exist in A but not in the other files. I was able to code a script so that File A is compared against 4 other files.
However, to improve code readability and compare File A against a variable number of files, I am trying to make the actual comparison process a subroutine so that it can be called the necessary number of times.
So my code takes in the first file and reads in every line, and makes a hash with the line as the key and its value 1. It then takes in each of the other files one by one and reads in the line from the file. The line that's read is used in a conditional statement to see if that key exists, and if it does, increments the value of that key by 1. In order to print only the differences of file A, I only print the hash keys that have values of 1.
The problem with my code is that although the array and hash is passed to the subroutine "compare," when the check to see if the key exists, the statement is always false for some reason. I believe the problem has something to do with the passing of the arguments into the subroutine because although I can access the array and hash successfully in the subroutine, the conditional statement is never satisfied.
So, I'm not sure if it's a perl syntax error or programmer but any help on this would be greatly appreciated.
I'm working on a script that takes in a certain file A and compares it against several other files and prints any lines that exist in A but not in the other files. I was able to code a script so that File A is compared against 4 other files.
However, to improve code readability and compare File A against a variable number of files, I am trying to make the actual comparison process a subroutine so that it can be called the necessary number of times.
So my code takes in the first file and reads in every line, and makes a hash with the line as the key and its value 1. It then takes in each of the other files one by one and reads in the line from the file. The line that's read is used in a conditional statement to see if that key exists, and if it does, increments the value of that key by 1. In order to print only the differences of file A, I only print the hash keys that have values of 1.
The problem with my code is that although the array and hash is passed to the subroutine "compare," when the check to see if the key exists, the statement is always false for some reason. I believe the problem has something to do with the passing of the arguments into the subroutine because although I can access the array and hash successfully in the subroutine, the conditional statement is never satisfied.
So, I'm not sure if it's a perl syntax error or programmer but any help on this would be greatly appreciated.
Code:
%var = (); while ($ln1 = <FH>) { # creates keys with first element of string (test name with no arguments) @ln1 = split /\s+/, $ln1; my $ln1 = shift @ln1; $var{$ln1}++; # value of each key set to 1 } @files = (file1, file2, file3, file4); foreach (@files) { open FILE, $_ or die "------ Could not open $_ testlist. \n"; @line = <FILE>; close FILE; &compare(@line, %var); } sub compare (\@\%){ my @line = shift @_; my %var = shift @_; foreach $ln (@line) { if (exists($var{$ln})) { $var{$ln}++; } } }
Comment