toadi has asked for the wisdom of the Perl Monks concerning the following question:
This demo code can be run... But as you can see the union doesn't work! It will show that there isn't a union of the 2 hashes but only a print of the last hash.use strict; use Tie::IxHash; my %login; { tie( my %csv, 'Tie::IxHash', 'one' => undef, 'two' => undef, 'three' => undef, 'four' => undef, 'five' => undef, ); $csv{one} = "11"; $csv{two} = "12"; $csv{three} = "13"; $csv{four} = "14"; $csv{five} = "15"; $login{1} = \%csv; } { tie( my %csv, 'Tie::IxHash', 'one' => undef, 'two' => undef, 'three' => undef, 'four' => undef, 'five' => undef, ); $csv{one} = "11"; $csv{two} = "12"; $csv{three} = "23"; $csv{five} = "25"; $login{2} = \%csv; }{ tie( my %csv, 'Tie::IxHash', 'one' => undef, 'two' => undef, 'three' => undef, 'four' => undef, 'five' => undef, ); $csv{one} = "31"; $csv{three} = "23"; $csv{four} = "34"; $csv{five} = "25"; my $match = "2"; my %union; while (my ($k,$v) = each %login) { if ($k eq $match) { print "MATCH\n"; while ( my ($k, $v) = each %$v ) { $union{$k} = $v; } while ( my ($k, $v) = each %csv ) { $union{$k} = $v; } print map{$_ ."=". $union{$_} ."\n"} keys %union; } else { print "NO MATCH\n"; while ( my ($k, $v) = each %$v ) { print $k ."=". $v."\n"; } } } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Union of 2 hashes doesn't work.
by Abigail-II (Bishop) on Jan 10, 2003 at 11:14 UTC | |
Re: Uniion of 2 hashes don't work.
by BrowserUk (Patriarch) on Jan 10, 2003 at 11:17 UTC | |
by toadi (Chaplain) on Jan 10, 2003 at 13:20 UTC |