basscakes has asked for the wisdom of the Perl Monks concerning the following question:

dudes--

what if one and only one of my arrays has any data, and it's a single element? e.g.

$self->{foo} = []; $self->{bar} = []; $self->{gotit} = [ 5 ];
the grep trick below will fail because $seen{5} will = 1. any ideas for dealing with this scenario with equal cleverness?

hi monks--

this might be a stupid way to do what i'm doing, but now i have to know. i have an object like this:

$self->{foo} = [ 1, 2, 3 ]; $self->{bar} = [ 4, 5, 3 ]; $self->{nerd} = [ 3, 4, 8 ]; $self->{geek} = 1; $self->{useless} = 1;

now i want to find the array elements in all of the above that are the same. but i don't know which accessors have arrays and which don't. they only will have arrays if i put something there interesting, but i have no idea which ones will be used at any given time. so i do this, assuming i do know all possible accessor names:

foreach(@accessors) { if( ref($self->{$_}) eq "ARRAY" ) { push @tocheck, $_; } }
How do I now compare the contents of all the arrays i want to check? kind of like what would do this magic:

foreach my $e ( all the self->{dontknow}->[]'s in @tocheck ) { $isect{$e}++ }

Is there something smarter I can do other than push @tocheck? Is this the time for me to finally understand map? I've only used eval for the first time in my life today...

Replies are listed 'Best First'.
Re: how do i find common elements of X number of arrays whose names i don't know?
by Zaxo (Archbishop) on Jun 28, 2004 at 08:22 UTC

    You can grep the values for ref eq 'ARRAY' to get an AoA,

    my @tocheck = grep { ref eq 'ARRAY' } values %{$self}; my %count; for (@tocheck) { my %uniq; @uniq{@$_} = (); for (keys %uniq) { $count{$_}++; } } my @in_all = grep { $count{$_} == @tocheck } keys %count;
    That uses %uniq to eliminate duplicate entries in each array. Otherwise we couldn't say from the count whether an element was in all the arrays.

    After Compline,
    Zaxo

Re: how do i find common elements of X number of arrays whose names i don't know?
by integral (Hermit) on Jun 28, 2004 at 08:10 UTC

    Firstly to find @accessors, you can use the keys function on the %$self hash.

    But I'd choose to push the value rather than the key so a loop over values(%$self) might make more sense.

    my @tocheck; for my $value (values %$self) { push @tocheck, $value if ref($value) eq 'ARRAY'; } my %seen; for my $array (@tocheck) { $seen{$_}++ for @$array; } return grep { $seen{$_} == @tocheck } keys %seen;

    So far we haven't used map (but we did use grep), but we can combine the two loops together so we don't need @tocheck as an intermediate.

    my ($n, %seen); for my $array (values %$self) { next unless ref($array) eq 'ARRAY'; # we're only interested in array +s $seen{$_}++ for @$array; $n++; } return grep { $seen{$_} == $n } keys %seen;

    Here's a solution using map to replace one of the loops (Update: But it doesn't solve the right problem, since it just checks for duplicates, rather than things appearing multiple times):

    my %seen; for my $value ( map { ref($_) eq 'ARRAY' ? @$_ : () } values %$self ) +{ $seen{$value}++; } return grep { $seen{$_} > 1 } keys %seen;

    --
    integral, resident of freenode's #perl
    
Re: how do i find common elements of X number of arrays whose names i don't know?
by broquaint (Abbot) on Jun 28, 2004 at 08:52 UTC
    If you don't mind some memory hungry list processing code ... Update: urk, misread the op's question, so here's code that actually makes sense
    use DDS; # aka Data::Dump::Streamer my %self = {}; $self->{foo} = [ 1, 2, 3 ]; $self->{bar} = [ 4, 5, 3 ]; $self->{nerd} = [ 3, 4, 8 ]; $self->{geek} = 1; $self->{useless} = 1; my @sets = map {{ map{$_=>undef} @$_ }} grep { 'ARRAY' eq ref } values %$self; my @uniq = grep { my $el = $_; @sets == grep { exists $_->{$el} } @sets; } map keys(%$_), @sets; Dump \@uniq;
    HTH

    _________
    broquaint