Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Array name with a Variable

by GrandFather (Sage)
on Apr 17, 2008 at 21:10 UTC ( #681301=note: print w/ replies, xml ) Need Help??


in reply to Array name with a Variable

Is something like this what you are after?

use strict; use warnings; use Set::Scalar; my @array1 = qw(c d e); my @array2 = qw(e f g h); my @array3 = qw(a b d); my @array4 = qw(s g h j k l); my @metaArray = (\@array1, \@array2, \@array3, \@array4, ); for my $first (0 .. $#metaArray - 1) { for my $second ($first + 1 .. $#metaArray) { my (%union, %intersect); $union{$_}++ && $intersect{$_}++ foreach @{$metaArray[$first]}, @{$metaArray[$second]}; my @intersect = sort keys %intersect; print "\@array$first, \@array$second: @intersect - "; print scalar @intersect, "\n"; } }

Prints:

@array0, @array1: e - 1 @array0, @array2: d - 1 @array0, @array3: - 0 @array1, @array2: - 0 @array1, @array3: g h - 2 @array2, @array3: - 0

Perl is environmentally friendly - it saves trees


Comment on Re: Array name with a Variable
Select or Download Code
Re^2: Array name with a Variable
by Anonymous Monk on Apr 18, 2008 at 05:34 UTC
    If one of the input arrays has a duplicate entry, a spurious intersection will be detected.

    An input array set of

    my @array1 = qw(c d e); my @array2 = qw(e f g h); # my @array3 = qw(a b d); # original code my @array3 = qw(a a b d); # modified for test my @array4 = qw(s g h j k l);

    Produces output of

    @array0, @array1: e - 1 @array0, @array2: a d - 2 @array0, @array3: - 0 @array1, @array2: a - 1 @array1, @array3: g h - 2 @array2, @array3: a - 1

    godevars may be dealing with input arrays that have unique elements; certainly, his example data has this characteristic. However, it is nowhere stated explicitly that this is the case, so I just thought I'd mention it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2015-07-06 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (80 votes), past polls