Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi monks! I have two arrays of hash references, I've put a single element of each of them below. In both arrays each hash contains a 'year', 'month', 'day', 'hhmm' key and value. I then have different keys and values in each array (data1 in array1 and data2 in array2).
$array1[0] = { 'day' => '02', 'year' => '2003', 'month' => '06', 'hhmm' => '0900', 'data1' => '5' }; $array2[0] = { 'day' => '02', 'year' => '2003', 'month' => '06', 'hhmm' => '0900', 'data2' => '6' };
What I'm trying to do is to merge the two arrays into one, so that if 'year', 'month', 'day', 'hhmm' all match then both 'data1' and 'data2' keys and values appear in the new array. The arrays are not the same size, as one contains data for every 30 minute, the other for every 5 minues. If there is no match between the two arrays I still want to keep the data in the new array.
$array3[0] = { 'day' => '02', 'year' => '2003', 'month' => '06', 'hhmm' => '0900', 'data2' => '6', 'data1' => '5' };
I first tried using a pair of nested loops, but unsuprisingly this was very slow! I'm now thinking of a more efficient way of doing this. The arrays are sorted. My current plan was to loop through the larger of the two arrays and create an index of the position of the first hour of each day. Then while looping through the smaller array lookup in this index to find a place to start looking in the larger array. However as I'm sure there are thousands of different ways to do this, I though I'd ask for some wisdom! So is there a better, simpler, quicker, prettier, or other way to do this? Thanks very much

In reply to Combining arrays of hashes by Anonymous Monk

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 having a coffee break in the Monastery: (3)
As of 2024-04-26 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found