Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Union of 2 hashes don't work.

by toadi (Chaplain)
on Jan 10, 2003 at 10:24 UTC ( #225757=perlquestion: print w/replies, xml ) Need Help??
toadi has asked for the wisdom of the Perl Monks concerning the following question:

YEsterday i posted a reference problem I have solved myself :) But now I'm stuck on another little problem:
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"; } } } }
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.
If I make a union that way in another small script, it works perfect. But when I do it in this bigger example it doens't work anymore. Suggestions?

My opinions may have changed,
but not the fact that I am right

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
    I fail to understand what the problem is. Both %$v contain the same set of keys: one two three four five. You unify the hashes by first filling %union with the key value pairs of %$v, and then of %csv. But since the latter hash contains all of the keys of the former, no trace of content of the former hash can be found.

    The program is doing exactly as I expect it to behave.


Re: Uniion of 2 hashes don't work.
by BrowserUk (Pope) on Jan 10, 2003 at 11:17 UTC

    It's really not easy to see what your code is trying to acheive? Why are you using Tie::ixHash's? Why do you create the hashes in such a long winded way?

    The value of $k in the if statement will take two values during the loop: 1 and 2. You then compare these against the constant value $match that has the value 2. So for the first iteration of the while loop you will take the false branch of the loop and the true branch for the second iteration?

    It's not clear at all what you mean by "union" of two (or in your case three) hashes? By definition, a hash will only support unique keys. All three of your hashes contain the same keys. If you union these into a single hash, you will end up with the same five keys, the values of which will be the last value assigned to the given key, so the final state of the hash %union will depend upon the order in which the contributing hashes are assigned to it as demonstrated by the code below.

    It might be easier in this case if you would describe the application you wish to use this for, in words rather than than code. We might then be able to suggest a better approach to the problem you are trying to solve.

    #! perl -slw use strict; use Data::Dumper; use Tie::IxHash; my %login; { tie my %csv, 'Tie::IxHash'; @csv{ qw[one two three four five] } = qw[11 12 13 14 15]; $login{1} = \%csv; } { tie my %csv, 'Tie::IxHash'; @csv{ qw[one two three four five] } = qw[11 12 23 undef 25]; $login{2} = \%csv; } print Dumper \%login; my %union = (%{$login{1}}, %{$login{2}}); print Dumper \%union; my %union = (%{$login{2}}, %{$login{1}}); print Dumper \%union;

    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

      Well I actually did it on a other way...

      - I had 2 queries from a db.
      - I wanted to write the results in a csv(fixed order so tie).
      - But in the first query i had a key login and if this key existed in second query I wanted the data from both queries as a union on one line in the csv.(both had common fields and unique fields to the both queries)

      I solved it on db-level:
      - I made a new table in oracle where i put the data from query one. Then do a insert or update(to login) with second query.(first i did it with tempory tables in memory, but dumped that idea).
      - Finally I read out that table and make the csv.

      This solution is also good for knowing on which day what data should be in that csv. If something went wrong with creating the csv and/or sending it to the outside party. I easily can redo this step, because of this table.

      For the ones who would be interested hacked a small sample, how i could do it on perl-level:
      # record 1 $login = "me"; $name = "toadi"; $email = ""; $contract = "SERVICE"; $csv{$login}{name}=$name; $csv{$login}{email}=$email; $csv{$login}{contract}=$contract; # record 2 $login = "u"; $name = "other one"; $email = ""; $contract = "Programming"; $csv{$login}{name}=$name; $csv{$login}{email}=$email; $csv{$login}{contract}=$contract; foreach $login ( keys %csv ) { print "$csv{$login}{name},$csv{$login}{email},$csv{$login}{rek +nr},$csv{$login}{contract}\n"; }
      So if the key exits, it will write over the needed values. If key not exits it wil make a new entry...

      My opinions may have changed,
      but not the fact that I am right

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://225757]
Approved by valdez
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-04-23 16:30 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (431 votes). Check out past polls.