By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

I cannot access hash of hash from outside function?

P: 2
Here my code, when I write "print $query{2}{ctf};", I got nothing but When I write inside of function, I got value....
How can I access it from outouf function? How can I send my %query hash to another function?

Expand|Select|Wrap|Line Numbers
  1. ,;&getfile("query63.txt");
  2. print $query{2}{ctf};
  3.  
  4. sub getfile   {
  5. open (FILE, $_[0]);
  6.  
  7. my @data = <FILE>;
  8. close (DATA);
  9.  
  10. for($i=0; $i<=$#data;$i++){
  11.  
  12. $index=$i;
  13.  
  14. @words=split(/\s+/,$data[$index]);
  15. $count=$#words;
  16.  
  17.  
  18. if($count==2)  {   
  19.  
  20. @words = split(/\s+/,$data[$index]);
  21. $count=0;
  22. $putword=$putword+1;
  23.  
  24. if ($words[2] != 0){
  25.  
  26.  $ctf=$words[1];
  27.  
  28. $df=$words[2];
  29.  
  30.  
  31. }
  32.  
  33.  
  34. }
  35.  
  36. else {
  37.  
  38. @words = split(/\s+/,$data[$index]);
  39.  
  40.  
  41. %query=($putword =>{ctf=>$ctf,
  42.                     df=>$df,
  43.                     $words[1]=>{doclen =>$words[2],
  44.                                 tf =>$words[3],
May 28 '10 #1
Share this Question
Share on Google+
6 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
@historysav
First, the code you posted is not complete. We can only see part of the hash definition. Also, we cannot see the beginning of your script. Are you using:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
If not, you need to use them, resolve any issues associated to those pragma usages and then repost your code.

Regards,

Jeff
May 28 '10 #2

P: 2
Hi this is whole code ..
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $putword=0;
  4. $q63=7;
  5.  
  6. &getfile("query63.txt");
  7.  
  8. sub getfile   {
  9. open (FILE, $_[0]);
  10.  
  11.  
  12. my @data = <FILE>;
  13. close (DATA);
  14.  
  15.  
  16. for($i=0; $i<=$#data;$i++){
  17.  
  18. $index=$i;
  19.  
  20.  
  21.  
  22. @words=split(/\s+/,$data[$index]);
  23. $count=$#words;
  24.  
  25.  
  26.  
  27. if($count==2)  {   
  28.  
  29. @words = split(/\s+/,$data[$index]);
  30. $count=0;
  31. $putword=$putword+1;
  32.  
  33.  
  34.  
  35. if ($words[2] != 0){
  36.  
  37.  $ctf=$words[1];
  38.  
  39. $df=$words[2];
  40.  
  41.  
  42. }
  43.  
  44.  
  45. }
  46.  
  47. else {
  48.  
  49. @words = split(/\s+/,$data[$index]);
  50.  
  51.  
  52.  
  53.  
  54.  %query{$putword}={ctf=>$ctf,
  55.                     df=>$df,
  56.                     $words[1]=>{doclen =>$words[2],
  57.                                 tf =>$words[3],
  58.                          }
  59.                     },
  60.        );
  61.  
  62.  
  63. #When I write here,I can get value..here inside the loop   
  64. #print $query{2}{1}{doclen};
  65.  
  66.  
  67.  
  68.  
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75. }
  76.  
  77. #when I write here,I got nothing,,this is outside of loop..
  78. print $query{2}{1}{doclen};But 
  79.  
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
May 28 '10 #3

Expert Mod 100+
P: 589
Your code has compilation errors.
C:\TEMP>perl -c historysav.pl
syntax error at historysav.pl line 54, near "%query{"
syntax error at historysav.pl line 59, near "},"
syntax error at historysav.pl line 81, near "But


}"
historysav.pl had compilation errors.
Add the 2 lines that Jeff showed and fix as many of the problems as you can and fix line indentation of your code blocks.

Then come post back with the results and additional questions as needed.
May 28 '10 #4

Expert
P: 80
First add "use strict;use warnings;use diagnostics;use Fatal qw/:void open close/;" at start of your code, after #!.
Jun 6 '10 #5

Expert Mod 100+
P: 589
chorny,

Please be a little more cautious and explicit when making suggestions.

Putting multiple statements on a single line is poor style and if the OP follows your suggestion as written, the code won't compile, because it broke the shebang line.
Jun 6 '10 #6

Expert
P: 80
Add these lines at start of your code, after #! line:

"use strict;" - you will need to declare all variables with "my" (or in some rare cases with "our"). It helps against typing variable names incorrectly.

"use warnings;" - prints very helpful warnings, without stopping program

"use diagnostics;" - explains error messages.

"use Fatal qw/:void open close/;" - automatically checks result of "open" and "close" when you forget to do it.
Jun 7 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.