Reputation: 37
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";
Upvotes: 2
Views: 315
Reputation: 49
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?
Upvotes: -3
Reputation: 385546
@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;
Upvotes: 4