Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Looping through arrays : are they equal ?

by wind (Priest)
on Apr 21, 2011 at 19:33 UTC ( #900703=note: print w/ replies, xml ) Need Help??


in reply to Looping through arrays : are they equal ?

I would code your error checking in a way that gave you meaningful error messages:

use strict; use warnings; my $refcomp = { 'Ad1' => [qw(0A78 0A9E 0AB9 0AC0)], 'Ad2' => [qw(0A78 0A9E 0AB9 0AC0)], }; my %devs; for my $key (keys %$refcomp) { for (@{$refcomp->{$key}}) { $devs{$_}{$key}++; } } while (my ($dev, $keys) = each %devs) { if (my @notfound = sort grep {! $keys->{$_}} keys %$refcomp) { warn "$dev not found in: " . join(',', @notfound) . "\n"; } }


Comment on Re: Looping through arrays : are they equal ?
Download Code
Replies are listed 'Best First'.
Re^2: Looping through arrays : are they equal ?
by ZlR (Chaplain) on Apr 23, 2011 at 13:12 UTC
    Thanks wind, this really saved the day :) I must say i copypasted it shamelessy into my script, given the deadlines i had ...

    Now that i look at it, i see a couple of great things :

    - the structure of %devs and the way it's built from %comp

    - the way you "each" through %dev : $keys is a hash ref, (with the adresses of a device as keys). I need to 'each' more, when i have refs as values !

    So, now, the grep. It's over the keys of refcomp, so $_ is an adress. Hence, if $keys->{$_} is not defined, it means that this device we are eaching on is missing for that adress. keys %comp has all adresses but keys %$keys only has the adress of a specific device.

    Honestly, i would never have found that myself, the way it works for any number of adresses by 'reversing' the hash and looking at it from the device ... it's brilliant :D
    It's also a very consistent way of looking at this specific system conf giving me a complete 'for sure' list of errors. Thanks !!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://900703]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2015-07-28 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls