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

Discussion in 'Perl Misc' started by inetquestion, Feb 3, 2008.

  1. inetquestion

    inetquestion Guest

    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
    }
    inetquestion, Feb 3, 2008
    #1
    1. Advertising

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

    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.
    Joost Diepenmaat, Feb 3, 2008
    #2
    1. Advertising

  3. inetquestion

    inetquestion Guest

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

    That fixed the problem. Thanks for the quick reply!
    inetquestion, Feb 3, 2008
    #3
  4. 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
    John W. Krahn, Feb 3, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Timkovich
    Replies:
    0
    Views:
    60
    Nick Timkovich
    Feb 25, 2014
  2. Skip Montanaro
    Replies:
    0
    Views:
    56
    Skip Montanaro
    Feb 25, 2014
  3. Peter Otten
    Replies:
    1
    Views:
    60
    Mark Lawrence
    Feb 25, 2014
  4. Peter Otten
    Replies:
    0
    Views:
    54
    Peter Otten
    Feb 25, 2014
  5. Tim Chase
    Replies:
    5
    Views:
    57
    Tim Chase
    Feb 26, 2014
Loading...

Share This Page