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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Uniion of 2 hashes don't work. by BrowserUk
in thread Union of 2 hashes don't work. by toadi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-24 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found