Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   perl - array functions (union, intersection, difference, aonly,bonly) input problems. (http://www.velocityreviews.com/forums/t906295-perl-array-functions-union-intersection-difference-aonly-bonly-input-problems.html)

inetquestion 02-03-2008 03:54 PM

perl - array functions (union, intersection, difference, aonly,bonly) input problems.
 
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
}

Joost Diepenmaat 02-03-2008 03:58 PM

Re: perl - array functions (union, intersection, difference, aonly, bonly) input problems.
 
inetquestion <inetquestion@hotmail.com> writes:

> @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);


m, q, s and y are operators. You really should quote your strings,
probably using qw() in this instance:

my @ArrayA = qw(m q s y);

You really should use strict.

http://perldoc.perl.org/strict.html

Joost.


inetquestion 02-03-2008 04:10 PM

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

That fixed the problem. Thanks for the quick reply!

John W. Krahn 02-03-2008 04:43 PM

Re: perl - array functions (union, intersection, difference, aonly,bonly) input problems.
 
Joost Diepenmaat wrote:
> inetquestion <inetquestion@hotmail.com> writes:
>
>> @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);

>
> m, q, s and y are operators. You really should quote your strings,
> probably using qw() in this instance:


And don't forget the x operator.

> my @ArrayA = qw(m q s y);
>
> You really should use strict.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall


All times are GMT. The time now is 01:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.