Rob Anderson wrote:

Hi Jeroen,



while (@values = $lcsr->fetchrow) {



This is probably the root of your problem every time you go through this loop, you are repopulating your array, put a my before @values.


Why do you think this is a problem? All the my would do is create a new lexical @values in every iteration.
What do you mean by repopulating @values? my @values = ... would also populate @values.
Am I missing something?





  $key = shift @values;
  $key.= shift @values;

push @$ref_to_a, [EMAIL PROTECTED];



I don't recognise this format, I'd have done this line as. push @$ref_to_a, [EMAIL PROTECTED];


[EMAIL PROTECTED] creates an anonymous arrayref by copying the values stored in @values. perldoc perlref, perldoc perllol


If you are going use this statement
push @{$ref_to_a}, [EMAIL PROTECTED];
your while condition must be while (my @values = ...)

If you are going to use
push @{$ref_to_a}, [EMAIL PROTECTED];
you can leave the while condition as while (@values = ...)

Reason: 'shallow copying' vs 'deep copying'
Read through this link
http://www.stonehenge.com/merlyn/UnixReview/col30.html





if ($old_key ne $key) {
$hash{$key} = $ref_to_a;
my $ref_to_a = undef;
}
$old_key = $key;
}




Having made these comments, I haven't really understood your logic or the
need for your, old_key values. I'd have done something like the following.

while (my @row_data = $lcsr->fetchrow ) {


while (my ($key1, $key2, @row_data) = $lcsr->fetchrow) {


   $key = shift @row_data;
   $key.= shift @row_data;

   # create top level array if this is the first time we've seen this key
   if (! defined $data{$key}) {$data{$key} = []}


This is not neccessary, when you assign to a non-existent hash key, perl will create the key for you



my $array_ref = $data{$key};
push(@$array_ref, [EMAIL PROTECTED]);



You can replace the code inside the while block with push (@{$data{$key1 . $key2}}, [EMAIL PROTECTED]);

Your final code will be
while (my ($key1, $key2, @row_data) = $lcsr->fetchrow) {
   push (@{$data{$key1 . $key2}}, [EMAIL PROTECTED]);
}

}


Hope this helps


Rob Anderson








-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to