"Sudarshan Raghavan" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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? >
No it's me, I didn't know you could get a anon ref of a copy of an array like this. I do now. Thanks > > > > > > > >> $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 = ...) > That's right, it did. > > If you are going to use > push @{$ref_to_a}, [EMAIL PROTECTED]; > you can leave the while condition as while (@values = ...) > > Yup, I realise that now > > Reason: 'shallow copying' vs 'deep copying' > Read through this link > http://www.stonehenge.com/merlyn/UnixReview/col30.html > Ta > > > > > > > > > >> 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 > I did need this line because of the way I was explictly getting my array ref. My solution doesn't work without this line. I didn't realise you can initialise undefined values to references in this manner. It the same as this... my $ref ; # ref to what? push(@$ref, "1"); # a ref to an array ... which I would never have done. I'd have done the explict... my $ref = []; # ref to an anon array push(@$ref, "1"); Having your posts ripped apart is always a learning experience :-$ Cheers, > > > > > > 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]