Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
So as I understand it, you want to dump the current results and start a new %HOH when the $key changes.

When setting something like this up there are 3 things to consider:
1. What is the steady state, normal thing causes the desired action?
2. How do we get it started? (what is special about it?)
3. How do we get it stopped? (what is special about it?)

One approach for is to dump the results and reset %HOH when the $key changes. To do that perhaps:

my $prev_key = ""; my %HOH; while ( a line read from file ) { ...get the current key .. code that you have already if ( $current_key ne $prev_key) { if ($prev_key ne "" ) #don't dump first time through! { ...dump the %HOH #this is the previous result } $prev_key = $current_key; ...%HOH=(); #reset! } ...processing of the current_key } ...dump the %HOH ... #the last record...
To solve question (2), the first time through the loop, I don't dump the %HoH unless there is data there - there is more than one possible way to test for this (keys %HOH) or ($prev_key ne "" ). If the thing that does the dump/final processing of this time slice has some code to not do anything if there is no data, there is no nested "if".

To solve question (3), after the loop ends due to lack of data, dump the final incarnation of the hash.

There is of course more than one way to accomplish what you need, but the 3 questions are always the same : (1)steady-state operation, (2)getting started, (3)getting finished.

I recommend writing the "steady-state", question (1) code first and then "fiddle with it" until questions (2) and (3) work out ok.


In reply to Re^5: Hashes of Hashes of Arrays? Is it possible? by Marshall
in thread Hashes of Hashes of Arrays? Is it possible? by capriguy84

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 contemplating the Monastery: (3)
As of 2024-04-25 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found