Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to juggle between two arrays that are not same size

by gasho (Beadle)
on Dec 19, 2007 at 17:11 UTC ( [id://657918]=perlquestion: print w/replies, xml ) Need Help??

gasho has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have two arrays different sizes but potentialy same elements. I would like to extract from Bigger array only elements common, please see my code and pseudo code bellow, Thanks
@Names=qw(A B C); @FullRecord=qw(A_1 D_4 C_3 F_6 B_2); foreach $el (@FullRecord) { @SplitArray=split(/\_/,$el); #Pseudo code #if @SplitArray[0] found in @Names then push $el into @DesiredRecord #Result is DesiredRecod =qw(A_1 C_3 B_2);
(: Life is short enjoy it :)

Replies are listed 'Best First'.
Re: How to juggle between two arrays that are not same size
by kyle (Abbot) on Dec 19, 2007 at 17:19 UTC
    use Data::Dumper; @Names=qw(A B C); @FullRecord=qw(A_1 D_4 C_3 F_6 B_2); @name_presence{@Names} = (); @DesiredRecord = grep { exists $name_presence{(split /_/)[0]} } @FullRecord; print Dumper \@DesiredRecord; __END__ $VAR1 = [ 'A_1', 'C_3', 'B_2' ];
Re: How to juggle between two arrays that are not same size
by mwah (Hermit) on Dec 19, 2007 at 18:23 UTC

    the solution presented by kyle is somehow idiomatic, this would be a good choice. You can use an alternative form w/regular expressions in order to solve a more complicated pattern problem.

    The regex-form of kyle's solution would be like:

    ... my @Names = qw'A B C'; my @FullRecord = qw'A_1 D_4 C_3 F_6 B_2'; # prepare temporary dictionary my %h; @h{@Names} = (); my @DesiredRecord = grep exists $h{ [/^([^_]+)/]->[0] }, @FullRecord; print "@DesiredRecord\n"; ...

    Regards

    mwa

Re: How to juggle between two arrays that are not same size
by jrsimmon (Hermit) on Dec 19, 2007 at 17:24 UTC
    Have you considered a hash?
    use strict; use warnings; my %Names = (A => 1, B => 1, C => 1); my %FullRecord = (A_1 => 1, D_4 => 1, C_3 => 1, F_6 => 1, B_2 => 1,); my @DesiredRecord = (); foreach my $el (keys(%FullRecord)) { my @SplitArray=split(/\_/,$el); unshift(@DesiredRecord, $el) if exists $Names{$SplitArray[0]}; } print "@DesiredRecord";
Re: How to juggle between two arrays that are not same size
by perlfan (Vicar) on Dec 19, 2007 at 20:33 UTC
    A straight forward way to do an intersection of two arrays (provided their elements are scalar values) is to create a hash using one array, where the keys correspond to the elements of that array. Then run over the other array, and if a key in the original hash corresponds with an element (i.e., it exists) then you've found an element in common.

    The following solutions ensures all unique items in the intersection, even if both arrays contain duplicate items:
    my @array1 = qw/a a b b c c d d/; my @array2 = qw/ c c d d e e f f g g/; my @array3 = (); my %filter = (); foreach (@array1) { $filter{$_} = 1; } foreach (@array2) { if (exists($filter{$_}) && 2 == ++$filter{$_}) { push(@array3,$_); } } print join(' ',@array3);
    If your arrays are do not contain scalar values, i.e., array/hash/sub refs, etc; then you can write a function to pass $_ through to serialize it first.
Re: How to juggle between two arrays that are not same size
by Anonymous Monk on Dec 19, 2007 at 17:25 UTC
    there are probably faster solutions, but if your arrays don't get too large nested loops would be acceptable:
    foreach $el (@Fullrecord) { @splitarray=split(/\_/,$el); foreach $nameel (@Names) { push (@DesiredRecord, $nameel) if ($nameel eq $splitarray[0]); } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://657918]
Approved by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-29 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found