Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl - array functions (union, intersection, difference, aonly,bonly) input problems.

Reply
Thread Tools

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

 
 
inetquestion
Guest
Posts: n/a
 
      02-03-2008
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
}
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-03-2008
inetquestion <> 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.

 
Reply With Quote
 
 
 
 
inetquestion
Guest
Posts: n/a
 
      02-03-2008

That fixed the problem. Thanks for the quick reply!
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-03-2008
Joost Diepenmaat wrote:
> inetquestion <> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Pass input Array value to function to calc different input value Susan Cranford Javascript 2 07-05-2005 02:53 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57