in reply to Re: Comparing an array against another array
in thread Comparing an array against another array

Yeah, definitely way easier Mr. Anonymous.

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!

Replies are listed 'Best First'.
Re^3: Comparing an array against another array
by jaldama (Acolyte) on Jan 18, 2012 at 16:56 UTC

    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