I cannot access hash of hash from outside function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • historysav
    New Member
    • May 2010
    • 2

    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?

    Code:
    ,;&getfile("query63.txt");
    print $query{2}{ctf};
    
    sub getfile   {
    open (FILE, $_[0]);
    
    my @data = <FILE>;
    close (DATA);
    
    for($i=0; $i<=$#data;$i++){
    
    $index=$i;
    
    @words=split(/\s+/,$data[$index]);
    $count=$#words;
                         
    
    if($count==2)  {   
    
    @words = split(/\s+/,$data[$index]);
    $count=0;
    $putword=$putword+1;
    
    if ($words[2] != 0){
    
     $ctf=$words[1];
    
    $df=$words[2];
    
    
    }
    
    
    }
    
    else {
    
    @words = split(/\s+/,$data[$index]);
    
    
    %query=($putword =>{ctf=>$ctf,
                        df=>$df,
                        $words[1]=>{doclen =>$words[2],
                                    tf =>$words[3],
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by historysav
    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?

    Code:
    ,;&getfile("query63.txt");
    print $query{2}{ctf};
    
    sub getfile   {
    open (FILE, $_[0]);
    
    my @data = <FILE>;
    close (DATA);
    
    for($i=0; $i<=$#data;$i++){
    
    $index=$i;
    
    @words=split(/\s+/,$data[$index]);
    $count=$#words;
                         
    
    if($count==2)  {   
    
    @words = split(/\s+/,$data[$index]);
    $count=0;
    $putword=$putword+1;
    
    if ($words[2] != 0){
    
     $ctf=$words[1];
    
    $df=$words[2];
    
    
    }
    
    
    }
    
    else {
    
    @words = split(/\s+/,$data[$index]);
    
    
    %query=($putword =>{ctf=>$ctf,
                        df=>$df,
                        $words[1]=>{doclen =>$words[2],
                                    tf =>$words[3],
    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:

    Code:
    use strict;
    use warnings;
    If not, you need to use them, resolve any issues associated to those pragma usages and then repost your code.

    Regards,

    Jeff

    Comment

    • historysav
      New Member
      • May 2010
      • 2

      #3
      Hi this is whole code ..
      Code:
      #!/usr/bin/perl
      
      $putword=0;
      $q63=7;
      
      &getfile("query63.txt");
      
      sub getfile   {
      open (FILE, $_[0]);
      
      
      my @data = <FILE>;
      close (DATA);
      
      
      for($i=0; $i<=$#data;$i++){
      
      $index=$i;
      
      
      
      @words=split(/\s+/,$data[$index]);
      $count=$#words;
      
                                                 
      
      if($count==2)  {   
      
      @words = split(/\s+/,$data[$index]);
      $count=0;
      $putword=$putword+1;
      
      
      
      if ($words[2] != 0){
      
       $ctf=$words[1];
      
      $df=$words[2];
      
      
      }
      
      
      }
      
      else {
      
      @words = split(/\s+/,$data[$index]);
      
      
      
      
       %query{$putword}={ctf=>$ctf,
                          df=>$df,
                          $words[1]=>{doclen =>$words[2],
                                      tf =>$words[3],
                               }
                          },
             );
             
             
      #When I write here,I can get value..here inside the loop   
      #print $query{2}{1}{doclen};
      
      
      
      
      
      }
      
      
      
      
      }
      
      #when I write here,I got nothing,,this is outside of loop..
      print $query{2}{1}{doclen};But 
      
      
      }

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        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.

        Comment

        • chorny
          Recognized Expert New Member
          • Jan 2008
          • 80

          #5
          First add "use strict;use warnings;use diagnostics;use Fatal qw/:void open close/;" at start of your code, after #!.

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            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.

            Comment

            • chorny
              Recognized Expert New Member
              • Jan 2008
              • 80

              #7
              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.

              Comment

              Working...