473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I cannot access hash of hash from outside function?

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
6 1860
numberwhun
3,509 Expert Mod 2GB
@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
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
RonB
589 Expert Mod 512MB
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
chorny
80 Expert
First add "use strict;use warnings;use diagnostics;use Fatal qw/:void open close/;" at start of your code, after #!.
Jun 6 '10 #5
RonB
589 Expert Mod 512MB
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
chorny
80 Expert
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

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

Similar topics

9
by: Paul | last post by:
Hi, VB.NET is saying the file I am creating is in use by another process and won't complete its task of moving the file to the specified destination folder. Here is my code (the main bit...
11
by: Jian H. Li | last post by:
Hello, As a beginner to C++, I need Your kind help to clarify the basic concept. The sample C++ code as following: int i; // A i++; // B, error int main() {
13
by: jt | last post by:
Being a newbie in C++ and comming from C, I can't find information on how to access a class member function outside its class. Below is a snippet of the class and its member function: look at...
9
by: bettina | last post by:
I have the following piece of code (symplified) require("lang.{$lang}.inc.php"); // I include the inc. files .... function decodify($technique) { // functions that give me back the value in...
2
by: Brian Smith | last post by:
Hi, I am trying to write text data to an xml file via an XMLTextWriter object. When I try to initialize the XmlTextReader object, I get an IOException: The process cannot access the file...
0
by: newms | last post by:
The following thread is related to a file access problem that seems to be a C# language problem. I have temporarily resolved it with vb.net as described below and thought it might be helpful to...
9
by: mvsguy | last post by:
I'm a Notes admin tasked with fixing an Access problem. I hope someone will be gracious enough to help. The database is getting a 3420, object not defined, and I need to find all the places...
3
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: ...
0
by: anreinor | last post by:
attached code-segment is used 1. calling without "privileged" works well with current local-SYSTEM-account 2. after setting "privileged"-flag I cannot access the local filesystem any longer,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.