Ask a Question related to PERL Beginners, Design and Development.
-
Jerry Preston #1
Compare two array's
Hi!,
I know that this is a no brainer, but I cannot not get it! I want to
compare two arrays and delete the same values in one array.
foreach $j ( @a1 ) {
foreach $p ( @a2 ) {
$a2[ $p ] = undef if $a1[ $j ] == $a2 $p ];
}
}
What is wrong and is there a better, PERL way to do this?
Thanks,
Jerry
Jerry Preston Guest
-
Compare 2 PDF
Hi, is there any way to automate the comparison of two PDFs from outside Acrobat? e.g. I mark 2 PDFs, right-click offers me COMPARE and after... -
compare
How can I stop a function in PHP? When comparing data I want to stop the function on a hit. Function compare($data) { For... -
compare excel
I have an application needs to merge two excel files and remove duplicates, is there a custom tag that I can use or any suggestions how I should go... -
Compare 2 documents
I use Compare two documents feature alot and it is very time consuming. I would like to find a way to automate this. The document path and file name... -
Compare strings
Hi, I'm trying to see if a certain string matches a predefined string so I've tried: -------------- If txtCondition3 = "NO THEFT COVER unless... -
Jan Eden #2
Re: Compare two array's
Maybe the missing opening bracket on the third line?
Being a beginner myself, I cannot recommend a better way.>$a2[$p] = undef if $a1[$j] == $a2[$p];
- Jan
Jerry Preston wrote:
-->Hi!,
>
>I know that this is a no brainer, but I cannot not get it! I want to
>compare two arrays and delete the same values in one array.
>
>foreach $j ( @a1 ) {
> foreach $p ( @a2 ) {
> $a2[ $p ] = undef if $a1[ $j ] == $a2 $p ];
> }
>}
>
>What is wrong and is there a better, PERL way to do this?
There are 10 kinds of people: those who understand binary, and those who don't
Jan Eden Guest
-
John W. Krahn #3
Re: Compare two array's
Jerry Preston wrote:
Hello,>
> Hi!,
No, but there is a Perl way to do it: :-)> I know that this is a no brainer, but I cannot not get it! I want to
> compare two arrays and delete the same values in one array.
>
> foreach $j ( @a1 ) {
> foreach $p ( @a2 ) {
> $a2[ $p ] = undef if $a1[ $j ] == $a2 $p ];
> }
> }
>
> What is wrong and is there a better, PERL way to do this?
@a2 = do {
my %seen;
@seen{ @a1 } = ( 1 ) x @a1;
grep !$seen{ $_ }, @a2;
};
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Rob Dixon #4
Re: Compare two array's
Jerry Preston wrote:
The first thing that's wrong is you're not starting with>
> I know that this is a no brainer, but I cannot not get it! I want to
> compare two arrays and delete the same values in one array.
>
> foreach $j ( @a1 ) {
> foreach $p ( @a2 ) {
> $a2[ $p ] = undef if $a1[ $j ] == $a2 $p ];
> }
> }
>
> What is wrong and is there a better, PERL way to do this?
use strict;
use warnings;
Your code doesn't work because you're iterating over the
values in the arrays instead of the indices. This will
work as you intended
foreach my $j (0 .. $#a1) {
foreach my $p (0 .. $#a2) {
$a2[$p] = undef if $a1[$j] == $a2[$p];
}
}
but you'll get warnings when undefined elements of
@a2 are being compared.
The Perl way to do it is to build a hash of the elements
of @a1 like this
my %a1 = map(($_, 1), @a1);
foreach (@a2) { $_ = undef if $a1{$_} };
HTH,
Rob
Rob Dixon Guest



Reply With Quote

