Update: I did a very poor job of naming in the above example. Normally I would call a hash by the names of the values it contains...instead of just %hash, %nodes would be better. Of course some descriptive name for "vg1:36" is far better that just "$key". With the good names, foreach my $machine_port (sort keys %nodes) is much more readable. I have no idea whether "machine_port" is right or not because I have no idea what "vg1:36" means in the real world.#!/usr/bin/perl use strict; use warnings; my %hash; while (my $line = <DATA>) { next if $line =~ /^\s*$/; #skip blanks chomp $line; my ($value, $key) = split ':',$line,2; push @{$hash{$key}},$value; } foreach my $key (sort keys %hash) { print "$key => @{$hash{$key}} ERROR Single Node!\n" if @{$hash{$ke +y}}<2; } =prints vg3:38 => node1 ERROR Single Node! vg3:40 => node2 ERROR Single Node! =cut __DATA__ node1:vg1:36 node2:vg1:36 node1:vg2:37 node2:vg2:37 node1:vg3:38 node2:vg3:40
HoA can be useful because multidimensional hashes require a loop for each dimension in order to traverse the entire HoHoH..
In reply to Re: Compare the values in the hash
by Marshall
in thread Compare the values in the hash
by perl_5eeker
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |