Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

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

A hash cannot have duplicate keys, whereas an array can. Hence, the key POSITION NAME is given a value of 9. but then 6 lines later on, the value associated with that key gets overwritten with the value 15. And the key RESULT is initially set to the value 10, but the later overwritten with the value 16. And so on for all the other missing values.

What you can do about it depends upon what you want to do with the hash afterwards. It maybe as simple as reversing the keys and values. The line number (position) will always be unique, so you could use that as the key and the text as the value, but that makes it little more useful than the array.

Perhaps what you want is

foreach my $i ( 0..$#data ) { push @{ $dataPos{ $data[$i] } }, $i; print "$data[$i] -- > $i\n"; }

Which will accumulate an array of positions associated with each value. It would dump something like

$VAR1 = { ' TOTAL QUALITY' => [ 8, 14 ], 'RESULT' => [ 10, 16 ], 'CREATOR' => [ 3 ], ' ' => [ 13, 19, 20 ], 'TOTAL QUALITY' => [ 8 ], 'WELL' => [ 0 ], 'SAMPLE ID' => [ 5 ], 'DISPENSATION ORDER' => [ 2 ], 'POSITION NAME' => [ 9, 15 ], 'QUALITY' => [ 11, 17 ], 'DESCRIPTION' => [ 6 ], 'ENTRY ID' => [ 1 ], 'CREATION DATE' => [ 4 ], 'EDITED' => [ 12, 18 ], 'RUN ID AND DATE' => [ 7 ] };

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: hash and array mismatch by BrowserUk
in thread hash and array mismatch by dana

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 browsing the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found