2

I have tried to get a list of the unique elements in the first array. (AKA: the elements in the first array that are NOT in the second array.) However my script returns the number of unique elements not the info in each element.

As a newbie to Perl, I know there are many nuances to the language. I have not seen how I am getting a number instead of a list of elements. The only research I have seen is how to get a number of unique elements and apparently, I have discovered another way.

Any help is appreciated. Below is the code.

#!/usr/bin/perl

use strict;
use warnings;
use v5.10;

use XML:Simple;
use LWP::Simple;
use List::Compare;

my @upd = system ("perl test.pl | grep '*.x86_64.rpm'");
my @inst = system ("rpm -qa");

@inst = join( '.rpm', @inst);

my $ls = List::Compare->new( {lists=> [\@upd, \@inst]} );
my @list = $ls->get_unique;
@list = $ls->get_Lonly;

say "@list";
  • 1
    FYI, `get_Lonly` is an alias for `get_unique`, so you should only call one or the other, not both. – ThisSuitIsBlackNot Jun 19 '15 at 18:12
  • @ThisSuitisBlackNot oops... I guess I had misread the alias quote for `get_Lonly`. Corrected! – CrazyEd38239 Jun 19 '15 at 18:54
  • Are you sure that `grep` invocation does what you want? It looks like you are mixing shell-style patterns with a regular expression tool. On my system (and most POSIXish systems), `grep '*.x86_64.rpm'` looks for a pattern with a literal asterisk in it... – pilcrow Jun 19 '15 at 21:40
  • @pilcrow: Well, I'm working on that right now. I had to change `grep` invocation to `grep '.*x86_64.rpm'`. – CrazyEd38239 Jun 19 '15 at 22:18

2 Answers2

4

@upd contains one element, the exit status of the shell that executed perl test.pl | grep '*.x86_64.rpm'. Similarly, @inst contains one element, the exit status of rpm. Perhaps you were trying to capture the output? Use backticks.

my @upd  = `perl test.pl | grep '*.x86_64.rpm'`;
chomp(@upd);

my @inst = `rpm -qa`;
chomp(@inst);

Also, the following is incorrect:

@inst = join( '.rpm', @inst);

It should be replaced with the following:

$_ .= '.rpm' for @inst;
ikegami
  • 367,544
  • 15
  • 269
  • 518
-3

Iterate through the list and check each element against every in the second list, if its not in the entire second list push it (or do whatever) to a new array

#!/usr/bin/perl

use strict;
use warnings;
use v5.10;
my @upd = ('cat', 'dog','mouse', 'fatcatsrul');
my @inst = ('cat', 'dog', 'mouse');
my @unique_elements_in_upd;
foreach(@upd)
{
    my $key= $_;
    if(!($key ~~@inst))
    {
        push(@unique_elements_in_upd, $key);
        print $key . "\n";
    }
}

output: fatcatsrul

check out this http://perlmaven.com/smart-matching-in-perl-5.10

also check out this comparison How fast is Perl's smartmatch operator when searching for a scalar in an array?

Community
  • 1
  • 1
ingueni
  • 49
  • 1
  • 1
  • 5
  • I was thinking of some kind of `foreach` or `if` statement. Other answers hinted at some "magical" mode function. I thought I was misusing or missing a mode. – CrazyEd38239 Jun 19 '15 at 18:11
  • 1
    That's O(N*M), a very poor approach. It can be done in O(N+M) – ikegami Jun 19 '15 at 18:11
  • @ikegami this is true, but how big is your list? i realize this is bestcase=worse, but unless your lists are massive..... – ingueni Jun 19 '15 at 18:17
  • 1
    Even for very modest lists of 100 items, we're talking about 50x more compares. It's a pretty good bet that List::Compare does it right, so the entire point of your answer it so recommend making the code much worse (seeing as it doesn't address the OP's problem at all!) – ikegami Jun 19 '15 at 18:22
  • @ingueni "how big is your list" `rpm -qa` returns a list of all RPMs installed on a system. On my machine, that's 596 results. And that's only one of the OP's two lists. – ThisSuitIsBlackNot Jun 19 '15 at 18:27
  • 910 on the machine of mine that uses `rpm`. There's no way to know what the other will return, but I suspect it will be in the same ballpark. So for TSIBN: 360,000 comparions vs 1,200 hash operations (300x more operations). For mine: 810,000 comparions vs 1,800 hash operations (450x more operations). – ikegami Jun 19 '15 at 18:28
  • @ThisSuitIsBlackNot, so it appears my understanding of smart match was off, it will not perform a comparison of N*M elements – ingueni Jun 19 '15 at 21:31