Re: Compare two array's
From: John W. Krahn (krahnj_at_acm.org)
Date: 01/07/04
- Next message: Alan: "Using File::Find to generate playlists"
- Previous message: Jan Eden: "Re: Compare two array's"
- In reply to: Jerry Preston: "Compare two array's"
- Next in thread: Rob Dixon: "Re: Compare two array's"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
To: beginners@perl.org Date: Wed, 07 Jan 2004 02:17:18 -0800
Jerry Preston wrote:
>
> Hi!,
Hello,
> 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?
No, but there is a Perl way to do it: :-)
@a2 = do {
my %seen;
@seen{ @a1 } = ( 1 ) x @a1;
grep !$seen{ $_ }, @a2;
};
John
-- use Perl; program fulfillment
- Next message: Alan: "Using File::Find to generate playlists"
- Previous message: Jan Eden: "Re: Compare two array's"
- In reply to: Jerry Preston: "Compare two array's"
- Next in thread: Rob Dixon: "Re: Compare two array's"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|