difference in arrays/hashes

Discussion in 'Perl Misc' started by Me, Aug 12, 2005.

  1. Me

    Me Guest

    I have two data sets from two sources, each set of results is stored in
    files which are ared into @file1 and @file2.

    "man perlfaq4" gives an exampe of an intersection of the two. My
    problems is, I need to know which elements of @file1 are not in @file2
    and the other way around. How can I keep track of this using the example
    from the man page.
     
    Me, Aug 12, 2005
    #1
    1. Advertising

  2. Me

    Paul Lalli Guest

    Me wrote:
    > I have two data sets from two sources, each set of results is stored in
    > files which are ared into @file1 and @file2.
    >
    > "man perlfaq4" gives an exampe of an intersection of the two. My
    > problems is, I need to know which elements of @file1 are not in @file2
    > and the other way around. How can I keep track of this using the example
    > from the man page.


    Set all the elements from one array as keys to a hash, with an
    arbitrary value (say 1). Loop through the second array, keeping track
    of which elements do not exists as keys to the hash. These elements
    are the difference of the second array "minus" the first. Then reverse
    the procedure (after clearing the hash) to find the opposite
    difference.

    The code given in the perlfaq you found (and *thank you* for searching
    the perlfaq before posting!) will not translate immediately to what you
    want, as it stores all elements from both arrays into the hash, and
    then loops through the elements of the hash, rather than either
    individual array.

    #!/usr/bin/perl
    use strict;
    use warnings;

    my @a1 = qw/a 1 b 2 c 3/;
    my @a2 = qw/3 c 4 d 5 e/;

    my %h;
    $h{$_} = 1 for @a1;
    my @diff = grep {!exists $h{$_} } @a2;

    print "Elements in a2 not in a1: @diff\n";

    %h = ();
    $h{$_} = 1 for @a2;
    @diff = grep {!exists $h{$_} } @a1;

    print "Elements in a1 not in a2: @diff\n";

    __END__

    Elements in a2 not in a1: 4 d 5 e
    Elements in a1 not in a2: a 1 b 2


    Paul Lalli
     
    Paul Lalli, Aug 12, 2005
    #2
    1. Advertising

  3. Me wrote:
    > I have two data sets from two sources, each set of results is stored in
    > files which are ared into @file1 and @file2.
    >
    > "man perlfaq4" gives an exampe of an intersection of the two. My
    > problems is, I need to know which elements of @file1 are not in @file2
    > and the other way around. How can I keep track of this using the example
    > from the man page.


    The most important part of the "perldoc -q intersection" answer is the
    first sentence: "Use a hash." ;-)

    The rest is an example, and you need to adopt it to fit your particular
    situation.

    my %count;
    foreach my $element (@file1, @file2) { $count{$element}++ }
    my @unique1 = grep $count{$_} == 1, @file1;
    my @unique2 = grep $count{$_} == 1, @file2;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 12, 2005
    #3
  4. Me wrote:
    > I have two data sets from two sources, each set of results is stored in
    > files which are ared into @file1 and @file2.
    >
    > "man perlfaq4" gives an exampe of an intersection of the two. My
    > problems is, I need to know which elements of @file1 are not in @file2
    > and the other way around. How can I keep track of this using the example
    > from the man page.


    My CPAN module List::Compare
    (http://search.cpan.org/dist/List-Compare/) provides a number of ways
    to do this. Look for the get_unique and get_complement methods and
    functions.

    Jim Keenan
     
    James E Keenan, Aug 12, 2005
    #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. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    566
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    161
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    277
    Matthias Wächter
    Sep 13, 2007
  4. Neela megha shyam Chivukula

    On Hashes - How the hashes printing works?

    Neela megha shyam Chivukula, May 27, 2009, in forum: Ruby
    Replies:
    4
    Views:
    241
    Markus Schirp
    May 28, 2009
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    214
Loading...

Share This Page