perl - array functions (union, intersection, difference, aonly,bonly) input problems.

I

inetquestion

The following perl script computes: union, difference, intersection,
and elements which only exist in each of the arrays passed as inputs.
However If either of the original arrays have the letters: m, q, s, or
y the script fails. Anyone have any suggestions as to what is going
on here?

-Inet






#!/usr/bin/perl

### Using the following letters in either array cause errors: m, q,
s, y

@ArrayA = (1, 3, 5, 6, 7, 8, a, b, c, d, e, f, g, h, i, j, k, l, o, p,
t, u, v, w, x, z);
@ArrayB = (2, 3, 5, 7, 9, x, z, n, r, t, u, v, A, B, C, D);

my ($union_ref, $isec_ref, $diff_ref, $aonly_ref, $bonly_ref) =
ArrayFunctions(\@ArrayA, \@ArrayB);

print "A: @ArrayA\n";
print "B: @ArrayB\n\n";
print "Union: @$union_ref\n";
print "Inter: @$isec_ref\n";
print "Diff: @$diff_ref\n";
print "Aonly: @$aonly_ref\n";
print "Bonly: @$bonly_ref\n";




sub ArrayFunctions
{
my $a_ref = shift; # reference to
input array A
my @a = @$a_ref; # input array
A
my $b_ref = shift; # reference to
input array B
my @b = @$b_ref; # input array
B

@Aseen{@a} = (); # lookup table
@Bseen{@b} = (); # lookup table

@isec = @diff = @union = @aonly = @bonly = (); # create null
arrays
foreach $e (@a, @b) { $count{$e}++ } # put all
items in hash table

foreach $e (keys %count) { # interate
over each key of hash table
push(@union, $e); # keys of hash
table = union
if ($count{$e} == 2) {
push @isec, $e; # seen more
than once = intersection
} else {
push @diff, $e; # seen once =
difference
push(@aonly, $e) unless exists $Bseen{$e}; # seen once +
from A = Aonly
push(@bonly, $e) unless exists $Aseen{$e}; # seen once +
from B = Bonly
}
}
return (\@union, \@isec, \@diff, \@aonly, \@bonly); # return
referecnes to computed arrays
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top