Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comparing Arrays -> storing results in a third Array

by FireBird34 (Pilgrim)
on Aug 14, 2002 at 23:42 UTC ( #190269=perlquestion: print w/ replies, xml ) Need Help??
FireBird34 has asked for the wisdom of the Perl Monks concerning the following question:

Ok, please don't bash the new guy here. I have been learning perl for awhile, but nothing serious. I am starting to learn more, while creating some self programs. However, I can't seem to find anything good on comparing arrays, to suite what I require. Pretty much, given:
@array_1 = ['x', 'y', 'z'] @array_2 = ['v', 'w', 'x']
I want to be able to compare these to arrays, and store the similar variables in each of them into @array_3 (which would be "x" in this case). I know this is pretty basic stuff here compared to the rest of these questions, so please don't bash. Hey, I'm learning! :)

Comment on Comparing Arrays -> storing results in a third Array
Download Code
Re: Comparing Arrays -> storing results in a third Array
by Zaxo (Archbishop) on Aug 14, 2002 at 23:53 UTC

    Run 'perldoc -q intersection' to find the question in perlfaq4

    After Compline,
    Zaxo

Re: Comparing Arrays -> storing results in a third Array
by djantzen (Priest) on Aug 15, 2002 at 00:56 UTC

    You might try something using grep such as:

    @array_1 = ('a', 'b', 'c'); @array_2 = ('c', 'd', 'e'); $list_1 = join '|', @array_1; @array_3 = grep { $_ =~ $list_1 } @array_2;
      Not the best way to go for a beginner, but hell this is neat ;-) ++
Re: Comparing Arrays -> storing results in a third Array
by GhodMode (Pilgrim) on Aug 15, 2002 at 02:37 UTC

    Try...

    #!/usr/bin/perl -w use strict; my @array_1 = ('a','b','c'); my @array_2 = ('x','y','c'); my $index1 = 0; my $index2 = 0; for ( @array_1 ) { # Reset the index of the second array because we're # going to read through it again $index2 = 0; my $element1 = $_; for ( @array_2 ) { my $element2 = $_; if ( $element1 eq $element2 ) { print "Index # $index2 of array_2 " . "($array_2[$index2]) = index # " . "$index1 of array_1 ($array_1[$index1])\n"; } $index2++; } $index1++; }
         This is probably not the most elegant solution, but I it will work.

    Here's the output...
    Index # 2 of array_2 (c) = index # 2 of array_1 (c)

    Invulnerable. Unlimited XP. Unlimited Votes. I must be...
            GhodMode
Re: Comparing Arrays -> storing results in a third Array
by DamnDirtyApe (Curate) on Aug 15, 2002 at 03:27 UTC

    I'd agree with using grep, though I'd use a hash rather than a regexp.

    my @array_1 = qw( x y z ) ; my @array_2 = qw( v w x ) ; my %base = map { $_ => 1 } @array_1 ; my @intersection = grep { $base{$_} } @array_2 ;

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: Comparing Arrays -> storing results in a third Array
by Necos (Friar) on Aug 15, 2002 at 08:52 UTC
    Just to show another slightly different approach:
    use strict; my @a1 = ( 'x','y','z'); my @a2 = ('z','b','c'); my @combined = @a1; push(@combined,$_) for @a2; my %dupes; $dupes{$_}++ for (sort @combined); my @results = map{if($dupes{$_} > 1) { $_; }}keys %dupes; print @results;
    Will print out 'z' as the final result. map is a VERY useful function. But, like the manpage says, please don't make map go through all the work to produce an array (it's return value) and then discard it (unless you are playing a game of Perl-golf... Then all bets are off... ^_~).

    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    4650 W. Olympic Blvd.
    Los Angeles, CA 90019
    323-937-3210 ext. 224
    email->secon_kun@hotmail.com
    perl -e "map{print++$_}split//,Mdbnr;"
Re: Comparing Arrays -> storing results in a third Array
by vek (Prior) on Aug 15, 2002 at 18:32 UTC
    You might also want to try davorg's Array::Compare.
    my $comp = Array::Compare->new(DefFull => 1); $comp->compare(\@arr1, \@arr2);
    Returns a list of elements which are different. Returns an empty list if the array's are the same.

    HTH

    -- vek --
Re: Comparing Arrays -> storing results in a third Array
by adrianh (Chancellor) on Aug 15, 2002 at 21:22 UTC

    Not that I'm recommending this as a real solution. However, if you are feeling slightly perverse.

    use strict; use warnings; use Quantum::Superpositions; my @array_1 = ('x', 'y', 'z'); my @array_2 = ('v', 'w', 'x'); my @array_3 = eigenstates( any(@array_1) eq any(@array_2) );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2014-07-13 02:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls