in reply to matching arrays

Making a whole heap of assumptions (e.g that those three terms are the only ones that will appear, that they will appear only once in a particular array, that regardless of order they arrays are the same, etc).
use strict; use warnings; my @occurances = ( [ 'male' ], [ 'male' ], [ 'male', 'child' ], [ 'female' ], [ 'child', 'female' ], [ 'female', 'child' ], [ 'female', 'male', 'child' ], ); my @terms = ( [ 'male' ], [ 'female' ], [ 'child' ], [ 'male', 'female' ], [ 'male', 'child' ], [ 'female', 'child' ], [ 'male', 'female', 'child' ], ); my %tally; foreach my $array_ref ( @occurances ) { $tally{stringify(@$array_ref)}++; } foreach my $array_ref ( @terms ) { if ( defined $tally{stringify(@$array_ref)}) { print "Terms:" . join (" and ", @$array_ref) . " were found " . $tally{stringify(@$array_ref)} . " times\n" } else { print "Terms:" . join (" and ", @$array_ref) . " were found 0 times\n" } } sub stringify { join ("+", sort {$b cmp $a} @_) }
Note that I did switch your hashes for arrays, as it made more sense to me to have them this way.

-enlil

Replies are listed 'Best First'.
Re: Re: matching arrays
by rsiedl (Friar) on May 25, 2004 at 21:41 UTC
    Thanks for the reply.

    The @occurrences is actually a hash of arrray's.
    push(@{$occurrences{$ID}}, "$term1"); push(@{$occurrences{$ID}}, "$term2");
    Do you know of an easier way rather than running your code within a foreach loop?
    foreach (keys (%occurrences)) { (@occurrences) = @{$occurrences{$_}}[ # Do the code you posted... } # end-foreach
Re: Re: matching arrays
by rsiedl (Friar) on May 25, 2004 at 22:19 UTC
    thanks enlil. I've made the few changes needed for it to fit into my script and it works like a charm.

    Cheers,
    Reagen