Re: Comparing an array against another array
by JavaFan (Canon) on Jan 17, 2012 at 16:47 UTC
|
| [reply] [d/l] |
|
How would I use something like this, though, to see what is in A but not in B? It doesn't matter if something else is in B that isn't in A.
| [reply] |
|
#!/usr/bin/perl
@A = qw/dog cat squirrel wolf/;
@B = qw/dog cat monkey donkey/;
undef @C{ @A };
delete @C{ @B };
say for keys %C;
__END__
prints:
squirrel
wolf
| [reply] [d/l] |
|
| [reply] |
|
I am using List::Compare, seemed like a good way to compare these two arrays. My comparing method doesn't seem to be producing the result it should be, though..
Would it be because doing ls -a stores each item on a new line?
To clarify, here is the code:
#!/usr/bin/perl
use List::Compare;
my $hostname = $ARGV[0];
my @hostFiles = qw/filecheck.pl hostscript.pl awesomeness.txt/;
my @output =`ssh $hostname "cd Desktop; ls -a"`;
$lc = List::Compare->new(\@hostFiles, \@output);
@Lonly = $lc->get_unique;
print @Lonly;
And this is what prints out
Password:
awesomeness.txt filecheck.pl hostscript.pl
So then I printed out what's on my desktop along with whatever @Lonly is saving as:
awesomeness.txt filecheck.pl hostscript.pl
.
..
.DS_Store
.localized
PERL TESTS
compare.pl
greptest.pl
hostfilecheck.pl
hostscript.pl
Why would @Loutput still be showing hostscript.pl if that's on the Desktop?
| [reply] [d/l] [select] |
Re: Comparing an array against another array
by tobyink (Canon) on Jan 17, 2012 at 17:26 UTC
|
Personally I wouldn't use Perl for this task unless it was something I knew I'd need to repeat over and over again. For a one-off comparison, I'd just use:
find -type f | sort > file-list.txt
... on each host to generate a lexically sorted list of files, and then compare the two lists using diff. | [reply] |
|
comm might be also useful here instead of diff.
| [reply] |
|
i have not referred to diff command of linux.
please see this
my @a = ( 1, 2, 3 );
my @b = ( 3, 4, 5 );
my @diff_a = @a->diff(\@b) # [ 1, 2 ]
| [reply] [d/l] |
|
|
|
|
|
Re: Comparing an array against another array
by choroba (Cardinal) on Jan 18, 2012 at 17:24 UTC
|
| [reply] |
Re: Comparing an array against another array
by Anonymous Monk on Jan 18, 2012 at 15:52 UTC
|
If you are looking for a known list of files, it is very handy to use a hash. Insert all of the expected file names into the hash, then remove each name as you encounter it. At the end of the traverse, the hash should be empty.
If dealing with a case-insensitive file system, normalize the names by shifting them to lowercase before inserting them or checking them.
| [reply] |
|
my @hostFiles = qw/filecheck.pl hostscript.pl awesomeness.txt/;
my @output =`ssh $hostname "cd Desktop; ls -a"`;
my %comparison;
for my $file (@hostFiles) {
$comparison{$file} +=1;
}
for my $file (@output) {
$comparison{$file} +=2
}
for my $file (sort keys %comparison) {
@missing = "Missing file: $file\n" if $comparison{$file} ==1;
print "Extra file: $file\n" if $comparison{$file} ==2;
}
Now I'm just working on filtering out duplicate results and I should be good, hurray!
| [reply] [d/l] |
|
What's going on:
I've ssh'd onto my localhost, ls the desktop and taken those items and put them into an array
I hardcoded a short list of items and I am comparing them with a hash to see if anything is missing from the host (See if something from a is NOT in b, and let me know).
So after figuring that out, when I print out the "missing files" I get a bunch of duplicates (see below), not sure if that has to do with how the files are being checked in the loop, but I figured the best thing to do would be to just sort out the data and eliminate dupes.
When I do THAT, and print out the fixed data, only one file is printing, two are missing. Any idea why?
#!/usr/bin/perl
my $hostname = $ARGV[0];
my @hostFiles = ("filecheck.pl", "hostscript.pl", "awesomeness.txt
+");
my @output =`ssh $hostname "cd Desktop; ls -a"`;
my %comparison;
for my $file (@hostFiles) {
$comparison{$file} +=1;
}
for my $file (@output) {
$comparison{$file} +=2
}
for my $file (sort keys %comparison) {
@missing = "$file\n" if $comparison{$file} ==1;
#print "Extra file: $file\n" if $comparison{$file} ==2;
print @missing;
}
my @checkedMissingFiles;
foreach my $var ( @missing ){
if ( ! grep( /$var/, @checkedMissingFiles) ){
push( @checkedMissingFiles, $var );
}
}
print "\n\nThe missing Files without dups:\n @checkedMissingFiles
+\n";
Password:
awesomeness.txt ##This is what is printing after comparing the two
+ arrays
awesomeness.txt
filecheck.pl
filecheck.pl
filecheck.pl
hostscript.pl
hostscript.pl
The missing Files without dups: ##what prints after weeding out duplic
+ates
hostscript.pl
| [reply] [d/l] |