Hi again, all... and apologies straight-up for the weirdo tiitle(!)
I'm now working on a project where I want to do some two-way checking for 'missing values' and I think the start of the process involves building some hashes.
I think the code below is a start... and is easier to deal with when compared to trying to see if an element I want to add to an anonymous hash already exists in that anonymous hash, even though there's a couple of lots of processing involved.
The code works Ok... but there are a couple of things still to be worked out:-
So... to the code:-
use Data::Dumper; %data_hash = (); %output_hash = (); while( <DATA> ) { # Build list of unique (sit +e:dsk) items ($site, $buf) = split(/,/, $_); @input_item = split(/:/, $buf); foreach $input_field (@input_item) { # EX: "VAR8=36!206!207!" @dsk_list = ($input_field =~ /([0-9]+)!([0-9]+)!$/); # Get last + 2 of 3 items foreach $dsk (@dsk_list) { # Each dsk item in the inpu +t... next if ($dsk == 0); # Skip '0' dsk items $key = $site . ":" . $dsk; # Build composite key $data_hash{$key}++; # ...and save it } } } foreach $key ( sort keys %data_hash ) { # Build list of dsk -> (mul +ti sites) ($site, $dsk) = split(/:/, $key); push( @{$output_hash{$dsk} }, $site ); } foreach $dsk (sort {$a <=> $b} keys %output_hash) { # Show list of si +tes for each dsk printf("Dsk: %d:\n", $dsk); foreach $site (sort {$a <=> $b} @{$output_hash{$dsk}}) { printf(" %d\n", $site); } printf("\n"); } __DATA__ 1108,VAR6=36!204!205!:VAR8=36!206!207!:VAR13=36!70!0!:VAR14=36!70!71!: +VAR15=36!71!0! 377,VAR12=36!97!96! 512,VAR6=36!90!91!:VAR8=36!92!93!:VAR11=36!0!70!:VAR12=36!189!190! 587,VAR2=36!550!0!:VAR4=36!554!0!:VAR6=36!551!0!
...and some example output:-
Dsk: 70: 512 1108 Dsk: 71: 1108 Dsk: 90: 512 Dsk: 91: 512 Dsk: 92: 512 Dsk: 93: 512 Dsk: 96: 377 Dsk: 97: 377 Dsk: 189: 512 Dsk: 190: 512 Dsk: 204: 1108 Dsk: 205: 1108 Dsk: 206: 1108 Dsk: 207: 1108 Dsk: 550: 587 Dsk: 551: 587 Dsk: 554: 587
Ultimately, I expect to use defined() to see if an element exists or not, which will let me display the 'missing items' I mentioned at the start... or I could use some sort of 'union/intersection' construct on the arrays of keys...
Would appreciate any clues on how to approach this...
Thanks...
In reply to How to Check Hashes for Missing Items when Keys can be Values and vice versa by ozboomer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |