Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Combining arrays of hashes
by ant9000 (Monk) on Jul 09, 2003 at 09:39 UTC
    I'd build a temporary hash before array3:
    %hash3 = map { $_->{year}.$->{month}.$_->{day}.$_->{hhmm}= $_ } @array1; for(@array2){ $k=$_->{year}.$->{month}.$_->{day}.$_->{hhmm}; if(!defined $tmp{$k}){ $hash3{$k}=$_; }else{ $hash3{$k}->{data2}=$_->{data2}; } } @array3=map { $hash3{$_} } sort keys %hash3; #or just @array3=values %hash3; #if you don't care about sorting

    That should be pretty fast if you use map on the larger array (I assumed it is @array1). HTH!
    (beware, the code is untested)
    if(!defined $tmp{$k}){

    should be
    if(!defined $hash3{$k}){

    Well, I'm learning by trial and error, it seems.
Re: Combining arrays of hashes
by broquaint (Abbot) on Jul 09, 2003 at 10:39 UTC
    If I understand your question correctly this should do it
    ## untested code follows ... my($max,$min) = @array1 > @array2 ? (\@array1, \@array2) : (\@array2, \@array1); my @keys = qw/ year month day hhmm /; my @newarray; for my $i (0 .. @$max) { last if $i >= @$min; if(@keys == grep { $array1[$i]->{$_} eq $array2[$i]->{$_} } @keys) { push @newarray => { %{ $array1[$i] }, data2 => $array2[$i]->{data2} }; } else { push @newarray => $array1[$i], $array2[$i]; } }



Re: Combining arrays of hashes
by Anonymous Monk on Jul 09, 2003 at 11:35 UTC
    Thanks very much guys, I'll have a play about with those!