in reply to Comparing an array against another array

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.

  • Comment on Re: Comparing an array against another array

Replies are listed 'Best First'.
Re^2: Comparing an array against another array
by choroba (Cardinal) on Jan 17, 2012 at 17:30 UTC
    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 ]

        You might not have, but I certainly did.

        diff seems to be just what I need, thanks a lot. Is my only option perl5i to use the diff method? Or does anyone know why I'm getting this error? Can't call method "diff" without a package or object reference at backtick.pl line 8.

        my $hostname = $ARGV[0]; my @hostFiles = ("filecheck.pl", "hostscript.pl"); my @output =`ssh $hostname "cd Desktop; ls -a"`; my @diff = @hostFiles->diff(\@output); . . .

        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?