Hi all,
I could use some help with the following problem:
I have rows of data (coming in from DBI) looking like this:
Key1, Key2, Date_from, Date_to, Value
1, 1, 01-10-2002, 31-10-2002, value1
1, 1, 01-11-2002, 30-11-2002, value2
1, 2, 01-10-2002, 31-10-2002, value3
1, 2, 01-10-2002, 30-10-2002, value4
1, 2, 01-11-2002, 31-11-2002, value5
2, 2, 01-10-2002, 30-10-2002, value6
2, 2, 01-11-2002, 31-11-2002, value7
What I want to do is: Create a hash of an array of an array, with this format:
The keys of the hash are the first 2 fields and the array of array is the rest of the
record.
Basically, what I want to do is aggravate on the first 2 fields.
So the hash looks like
$hash{1, 1} = reference to array of array
([ 01-10-2002, 31-10-2002, value1],
01-11-2002, 30-11-2002, value2])
$hash{1, 2} = reference to array of array
([01-10-2002, 31-10-2002, value3]
[01-10-2002, 30-10-2002, value4]
[01-11-2002, 31-11-2002, value5])
etc
Ok, so far so good.
This is a code snippet:
my $old_key = undef;
my $key = undef;
my $ref_to_a = undef;
while (@values = $lcsr->fetchrow) {
$key = shift @values;
$key.= shift @values;
push @$ref_to_a, [EMAIL PROTECTED];
if ($old_key ne $key) {
$hash{$key} = $ref_to_a;
my $ref_to_a = undef;
}
$old_key = $key;
}
The problem is: the reference $ref_to_a is not new all the time.
The 2 dimensional array is growing all the time but I want a new 2 dimensional array
for each hash key.
In other computer languages is is possible to do something like 'new' or 'malloc' and
create a new reference
How do I make sure a new 2dim array is created for each new key value.
Thanks in advance.
PS I want this data structure as I want to be able to lookup data in date ranges.
So imagine: I want to find the data record in which this key falls: 1, 2, 15-11-2002
It should return value5 as that is within the date range and key.
The 2dim array is to implement a binary search as a straight forward hash wouldn't
work.
But maybe my whole solution to the problem is wrong anyway, maybe there is a more
clever way.
So feel free to comment on the algorithm as well
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]