Scarborough has asked for the wisdom of the Perl Monks concerning the following question:
Thanks for all your help with my last dereferencing problem and sorry that my code was not very clear, I was working that day with a Leffe (Belgium Beer) hang over, I wasn't to sharpe. I belived that I had hashes and references clear until today when I tried the following.
I am working on testing some perl scripts, translated by someone else, which replace SCL main frame code on a new server. The scripts replicate the convention of a variable having a name, localname and default value. I have also xml files with data about the scripts from which I can extract the data I need to run my testing programs. I have come up with the following code.
In my calling scriptsub make_var_hash{ my $xml = @_[0]; my %myhash; my $name; my $localname; my $defaultval; # #read the xml file extract the data and add to the following hash #UPDATE sorry this line should be $myhash{$name} = [$localname,$defaultval]; #NOT $myhash{$name} = ($localname,$defaultval); #end the loop return \%myhash; }
Works fine with the following result$hash1 = make_var_hash('MOR2305.xml'); foreach $key (keys($hash1)){ print "$key = $$hash1{$key}[0] & $$hash1{$key}[1]\n"; }
Just what I needed!START = MOR_START & 60 FINISH = MOR_FINISH & 150 etc....
However back in my calling script I started to get some starnge results and to stop a long story getting longer I changed above to.sub get_suite_vars{ my %suite; my ($job_vars, $job); my @jobs = ('MOR2305.xml', 'MOP4312.xml', 'IN543.xml'); foreach $job ($job_vars){ $job_vars = make_var_hash($job); $suite{$job} = $job_vars; } return \$suite; }
Which producedsub get_suite_vars{ my %suite; my ($job_vars, $job); my @jobs = ('MOR2305.xml', 'MOP4312.xml', 'IN543.xml'); foreach $job ($jobs){ $job_vars = make_var_hash($job); #UPDATE for anyone interested the error was here #In my original code I had foreach $key($job_vars){ #SORRY foreach $key (keys($job_vars)){ print "$key = $$job_vars{$key}[0] & $$job_vars{$key}[1]\n"; } $suite{$job}=$job_vars; } return \$suite; }
Not what the doctor ordered. Whats happening? The code has been shortened a little but in principle is the same. Any help greatfully excepted.START = MOR_START & ARRAY(0x1930fo30) = & FINISH = MOR_FINISH & ARRAY(0x1931eaf0) = & etc....
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: More dereferencing
by geekgrrl (Pilgrim) on May 04, 2004 at 16:02 UTC | |
by Scarborough (Hermit) on May 04, 2004 at 16:42 UTC | |
|
Re: More dereferencing
by dave_the_m (Monsignor) on May 04, 2004 at 16:04 UTC | |
by Scarborough (Hermit) on May 04, 2004 at 16:40 UTC |