CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
hash of hashes disappears

 

First page Previous page 1 2 Next page Last page  View All


efoss
Novice

Jul 22, 2009, 3:54 PM

Post #26 of 31 (2034 views)
Re: [FishMonger] hash of hashes disappears [In reply to] Can't Post


In Reply To
That's not the output I was requesting, but it does prove that the var does not "disappear".

Are you using the perl debugger (i.e., do you execute it as 'perl -d yourscript.pl') or are you using some other 3rd party debugger?

Remove the print statement that I had you put in and add this to the end of the script.

Code
END { 
print Dumper \%type_normalName_cutoff_chromosomes;
}

If you get the same output, then that proves that the var is intact to the end of the script.

If your debugger tells you that it doesn't exist or that it's empty, then either your debugger has a bug or your usage of it is flawed.


OK - I did that, and the result is the same, i.e. my variable appears to be there. However, the output file that I generated is not consistent with that - it's empty - and that result is consistent with what I see in the debugger. And if I run the script from a terminal rather than in my debugger, the output file is again inconsistent with the variable existing (i.e. the output file is empty).

The debugger I'm using is third party (Komodo) and I'm running it on Mac OS X.


FishMonger
Veteran / Moderator

Jul 22, 2009, 4:15 PM

Post #27 of 31 (2031 views)
Re: [efoss] hash of hashes disappears [In reply to] Can't Post


In Reply To

In Reply To
That's not the output I was requesting, but it does prove that the var does not "disappear".

Are you using the perl debugger (i.e., do you execute it as 'perl -d yourscript.pl') or are you using some other 3rd party debugger?

Remove the print statement that I had you put in and add this to the end of the script.

Code
END { 
print Dumper \%type_normalName_cutoff_chromosomes;
}

If you get the same output, then that proves that the var is intact to the end of the script.

If your debugger tells you that it doesn't exist or that it's empty, then either your debugger has a bug or your usage of it is flawed.


OK - I did that, and the result is the same, i.e. my variable appears to be there. However, the output file that I generated is not consistent with that - it's empty - and that result is consistent with what I see in the debugger. And if I run the script from a terminal rather than in my debugger, the output file is again inconsistent with the variable existing (i.e. the output file is empty).

The debugger I'm using is third party (Komodo) and I'm running it on Mac OS X.


I have not analyzed your code in detail, but it seems to be clear that the problem is with the loop and how you're accessing the hash and not that the var "disappears".

The starting point really should be to get rid of as many of those global vars as possible and declare them in the smallest scope that they require.


FishMonger
Veteran / Moderator

Jul 22, 2009, 4:41 PM

Post #28 of 31 (2027 views)
Re: [efoss] hash of hashes disappears [In reply to] Can't Post

Here are a few comments on a couple items in your code.


Code
$chromosome_length{1} = 230208; 
$chromosome_length{2} = 813178;
etc

That tells me that you should be using an array, not a hash. Whenever you have sequentially numbered hash keys like you're using, you know you used the wrong data structure.


Code
while ($chromosome <= 16) { 
$genomeLength += $chromosome_length{$chromosome};
$chromosome++;
}

would be better written as

Code
for my $i (1..16) { 
$genomeLength += $chromosome_length{$i};
}



Code
opendir (DIR, "$path");

Whenever you create a filehandle or directory handle, you should ALWAYS check the return code and take action if the open call fails. You should also use a lexical var for the handle and in the case of a filehandle, use the 3 arg form of open. Do not quote single vars.

see:
perldoc -q quoting


Code
opendir my $DIR, $path or die "opendir failed on '$path' $!";



(This post was edited by FishMonger on Jul 22, 2009, 4:43 PM)


efoss
Novice

Jul 22, 2009, 6:34 PM

Post #29 of 31 (2023 views)
Re: [FishMonger] hash of hashes disappears [In reply to] Can't Post


In Reply To
Here are a few comments on a couple items in your code.


Code
$chromosome_length{1} = 230208; 
$chromosome_length{2} = 813178;
etc

That tells me that you should be using an array, not a hash. Whenever you have sequentially numbered hash keys like you're using, you know you used the wrong data structure.


Code
while ($chromosome <= 16) { 
$genomeLength += $chromosome_length{$chromosome};
$chromosome++;
}

would be better written as

Code
for my $i (1..16) { 
$genomeLength += $chromosome_length{$i};
}



Code
opendir (DIR, "$path");

Whenever you create a filehandle or directory handle, you should ALWAYS check the return code and take action if the open call fails. You should also use a lexical var for the handle and in the case of a filehandle, use the 3 arg form of open. Do not quote single vars.

see:
perldoc -q quoting


Code
opendir my $DIR, $path or die "opendir failed on '$path' $!";



Thanks. I appreciate the comments.

I disagree with the first comment but it's based on biological stuff rather than good perl practice. Basically, those numbers are just like names. 1 means as much as "Fred" would in a different situation, so I think a hash is most appropriate and least likely to lead to errors.

I see the point of the second one. It also fits in to your general criticism of my use of global variables, something that I'll definitely consider.

I see a lot of the point of the third one, but I don't understand the "$!". What is that?

Thanks again for the comments. I definitely have a lot to learn in perl.


FishMonger
Veteran / Moderator

Jul 22, 2009, 7:54 PM

Post #30 of 31 (2021 views)
Re: [efoss] hash of hashes disappears [In reply to] Can't Post


Quote
I disagree with the first comment but it's based on biological stuff rather than good perl practice.


An array is an ordered list numerically indexed.

A hash is an unordered list which is string indexed.

A hash uses far more memory to hold it's data and since you're working with a large file that is loaded into memory, you should be mindful of the unneeded memory usage.

Following or not following Perl's best coding practices is the difference between good Perl programmers and mediocre or poor Perl coders.


Quote
I see a lot of the point of the third one, but I don't understand the "$!". What is that?


$! holds the error message returned by the OS. So, in the case of opening a filehandle or directory handle, it will tell you the OS's reason why it failed. You can learn about Perl's special variables by reading the perldocs that come with Perl, specifically:

Quote
perldoc perlvar

or online
http://perldoc.perl.org/perlvar.html


efoss
Novice

Jul 22, 2009, 8:09 PM

Post #31 of 31 (2017 views)
Re: [FishMonger] hash of hashes disappears [In reply to] Can't Post


In Reply To

Quote
I disagree with the first comment but it's based on biological stuff rather than good perl practice.


An array is an ordered list numerically indexed.

A hash is an unordered list which is string indexed.

A hash uses far more memory to hold it's data and since you're working with a large file that is loaded into memory, you should be mindful of the unneeded memory usage.

Following or not following Perl's best coding practices is the difference between good Perl programmers and mediocre or poor Perl coders.


Quote
I see a lot of the point of the third one, but I don't understand the "$!". What is that?


$! holds the error message returned by the OS. So, in the case of opening a filehandle or directory handle, it will tell you the OS's reason why it failed. You can learn about Perl's special variables by reading the perldocs that come with Perl, specifically:

Quote
perldoc perlvar

or online
http://perldoc.perl.org/perlvar.html


Thanks. That helps. Like I said, I have a lot to learn about perl - and programming in general, for that matter.

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives