Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comparing an array against another array

by jaldama (Acolyte)
on Jan 17, 2012 at 16:35 UTC ( [id://948350]=perlquestion: print w/replies, xml ) Need Help??

jaldama has asked for the wisdom of the Perl Monks concerning the following question:

Basically I am checking files on a host to make sure everything is there. So I am thinking, I will hardcode whatever files should be there into an array and compare that array to whatever is actually in that directory with 'ls' I looked into grep but that doesn't seem to be the function I should use, right? The problem with that is it filters out what ISN'T there, but I need to know what items aren't there to create some type of report. Any advice? --Update, so I ended up using List::Compare which was very simple and wonderbar. Thanks for the advice everyone!
  • Comment on Comparing an array against another array

Replies are listed 'Best First'.
Re: Comparing an array against another array
by JavaFan (Canon) on Jan 17, 2012 at 16:47 UTC
    perldoc -q intersect
      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.
        #!/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

        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?

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.

      comm might be also useful here instead of diff.
        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 ]
Re: Comparing an array against another array
by choroba (Cardinal) on Jan 18, 2012 at 17:24 UTC
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.

      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!

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://948350]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found