I was perturbed by your post Sardushan. I'll try to explain why.
Wait a second, my post was not an attempt to undermine Rob Anderson's post in any manner. I apologize if the wrong message came out. I sincerely hope that this does not turn Rob away from this list. Giving an answer without some explanation or a reference to one might lead to misconceptions. Trust me, I have developed some of my own (I am sure I still have some) and burnt my fingers posting them on this list as answers. But, I guess that's how you learn. I am not saying Rob Anderson's post is the result of a misconception, just saying this in general terms. Maybe I should start using more smileys in my mails.
You spelt my name wrong :-) (here you go)
Sudarshan Raghavan wrote:
Rob Anderson wrote:
Hi Jeroen,Why do you think this is a problem? All the my would do is create a
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.
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.
Rob also mentioned in his mail that he was not familiar with the [EMAIL PROTECTED] construct. You don't need the my if you are using the anonymous array construct.
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.
The trouble is, repopulating is not the problem, it is the fact that you are taking the reference to the same array every time that is the problem.
$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.
I didn't say that his method was wrong, all I said was his reason to the OP's problem is not quite right. What I was mentioning here are two scenarios where you need to be careful when using references and why you need to be careful.
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.
I apologize, I should have mentioned that :-)
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.
I don't expect all computer scientists to know these terms or understand what they mean. In this context, it is important that they understand them. If they don't this can be treated as a start.
They're useful terms only in the context of the document you have linked to.
I am not sure about that. These things (shallow copying, deep copying, autovivificationcan ...) can come back and haunt you big time when you are playing around with references. It is important you understand them to able to track down your problems. If you understand them, they can be very useful.
while (my ($key1, $key2, @row_data) = $lcsr->fetchrow) {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 ) {
Doesn't recreate the existing functionality without a subsequent
my $key = $key1.$key2
Yes it doesn't, but you can avoid the two shift statements
This is not neccessary, when you assign to a non-existent hash key,$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} = []}
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
What is $array_def?
push @{undef}, [EMAIL PROTECTED]
doesn't work.
Take a look at this code example use strict; use warnings; use Data::Dumper;
my $var = undef; #Not neccessary to initialize with an undef, just to make it explicit
push (@{$var->{'key1'}}, 1);
print Dumper($var);
Reason: autovivification Please read through this link http://tlc.perlarchive.com/articles/perl/ug0002.shtml
You can replace the code inside the while block withmy $array_ref = $data{$key}; push(@$array_ref, [EMAIL PROTECTED]);
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'
Yes this is a possibility. But, the OP's requirements were to form a key in this manner. Thanks for catching this possibility anyways :-)
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'.
As I have mentioned before, I am not trying to undermine anybody in this list. I don't agree with the detailed analysis part though. It is not just important to give solutions, if possible it is also important to explain why things need to be done in a certain way. This is a beginners list, care must be taken to avoid spreading misconceptions.
I hope this post itself falls into the last category - I shall no doubt find out if I'm wrong.
Yes it did, thanks for bringing this up. Gave me a oppurtunity to clear the air :-)
Cheers,
Rob
Cheers, Sudarshan
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]