I was perturbed by your post Sardushan. I'll try to explain why. Sudarshan Raghavan wrote: > 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.
Yes, but I can see why Rob thought that it may be a problem. I'm sure you can too given the rest of his post. > What do you mean by repopulating @values? my @values = ... would > also populate @values. > Am I missing something? You're missing the rest of his post, which we know you have seen since you comment on it below. As Rob suspected, repopulating the global array '@values' would be a problem if the loop pushed [EMAIL PROTECTED] onto the stack each time. > > > > > > > > > > $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 = ...) See? You knew all along really. Rob was trying his best to help without being used to using an anonymous array constructor like this. His answer was valid and he shouldn't be told otherwise. > If you are going to use > push @{$ref_to_a}, [EMAIL PROTECTED]; > you can leave the while condition as while (@values = ...) ...but no mention of all the more useful caveats that using a lexical is preferable since its scope is local to the containing block. > Reason: 'shallow copying' vs 'deep copying' > Read through this link > http://www.stonehenge.com/merlyn/UnixReview/col30.html How many computer scientists do you think would know what you were talking about if you mentioned 'shallow' and 'deep' copying. They're useful terms only in the context of the document you have linked to. > > > > > 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) { Doesn't recreate the existing functionality without a subsequent my $key = $key1.$key2 > > $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 It is necessary, because the following lines won't work without it. You need push @{$data{$key}},[EMAIL PROTECTED] instead, otherwise $array_def will be 'undef' and push @{undef}, [EMAIL PROTECTED] doesn't work. > > > > 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]); ...and hope very hard that you don't get two separate instances where $key1 = 'AB', $key2 = 'C' and $key1 = 'A', $key2 = 'BC' > Your final code will be Whose? > while (my ($key1, $key2, @row_data) = $lcsr->fetchrow) { > push (@{$data{$key1 . $key2}}, [EMAIL PROTECTED]); > } > This is paramountly a beginners group. I think Rob did a grand job of helping here, and may well have solved Jeroen's problem without further gloss. I believe there is a place for detailed analyses but only for interest's sake. I would hope that I and all other posters will only ever 'enquire', 'help', 'inform' or 'correct'. I hope this post itself falls into the last category - I shall no doubt find out if I'm wrong. Cheers, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]