basscakes has asked for the wisdom of the Perl Monks concerning the following question:
what if one and only one of my arrays has any data, and it's a single element? e.g.
the grep trick below will fail because $seen{5} will = 1. any ideas for dealing with this scenario with equal cleverness?$self->{foo} = []; $self->{bar} = []; $self->{gotit} = [ 5 ];
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:
How do I now compare the contents of all the arrays i want to check? kind of like what would do this magic:foreach(@accessors) { if( ref($self->{$_}) eq "ARRAY" ) { push @tocheck, $_; } }
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 | |
|
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 | |
|
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 |