ZlR has asked for the wisdom of the Perl Monks concerning the following question:
Here i am once again with a desperate attempt to sort my way through weird system configs with Perl.
At this point i have a hash where the values are a list of references to arrays. It's not really interesting what's inside, but maybe that structure is not the best one for my problem, or maybe you're just curious, so : the keys are unique adresses and each list is a bunch of devices mapped to it. I need to make sure that every device is in every list, that is that all these list are equals.
It should be easy, but i find myself doing this in an ugly way.
Here's what my hash looks like :
It could be that there is only one key, or more than 2. Also, in the end, i'm looping through a structure to build this hash, but let's forget that.$VAR1 = { 'Ad1' => [ '0A78', '0A9E', '0AB9', '0AC0', ], 'Ad2' => [ '0A78', '0A9E', '0AB9', '0AC0', + ] };
Here's what i do :
#!perl use strict ; use warnings ; my $refcomp= { 'Ad1' => [ '0A78', '0A9E', '0AB9', '0AC0', ], 'Ad2' => [ '0A78', '0A9E', '0AB9', '0AC0', + ] }; my %comp =%$refcomp ; # my code uses a hash, not a ref, so ... my %found ; if (scalar keys %comp > 1 ) { for my $adress (keys %comp) { for my $dev ( @{$comp{$adress}} ) { $found{$dev}++ } } } my %uniq ; for my $it (values %found) { $uniq{$it}++ } ; if ( scalar keys %uniq > 1 ) { print "ERR : Problem in device list\n" ; }
Anyone has a nicer way ? Thanks !
zlr
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Looping through arrays : are they equal ?
by wind (Priest) on Apr 21, 2011 at 19:33 UTC | |
by ZlR (Chaplain) on Apr 23, 2011 at 13:12 UTC | |
|
Re: Looping through arrays : are they equal ?
by Util (Priest) on Apr 21, 2011 at 19:24 UTC | |
by Util (Priest) on Apr 21, 2011 at 19:26 UTC | |
|
Re: Looping through arrays : are they equal ?
by kennethk (Abbot) on Apr 21, 2011 at 19:27 UTC | |
|
Re: Looping through arrays : are they equal ?
by anonymized user 468275 (Curate) on Apr 22, 2011 at 15:30 UTC | |
by ZlR (Chaplain) on Apr 23, 2011 at 13:15 UTC |