Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Problem of context?

by hippo (Archbishop)
on Oct 16, 2014 at 08:37 UTC ( [id://1104020]=note: print w/replies, xml ) Need Help??


in reply to Problem of context?

You have declared @test2 inside an if block inside the first while loop. As soon as execution leaves the if block the array goes out of scope. See Coping with Scoping for more details.

Update: Example - compare these two:

use strict; my $foo = 0; for (1..5) { $foo += $_; } print $foo;

and

use strict; for (1..5) { my $foo = 0; $foo += $_; } print $foo;

In each case the scope of $foo ends at the end of the block enclosing its declaration. Since the first example has the declaration outside the loop the scope persists to the end of the script. In contrast, the second example gives $foo scope only until the end of the for loop thus making the $foo in the print statement undeclared which will be trapped at compile time.

Replies are listed 'Best First'.
Re^2: Problem of context?
by pabla23 (Novice) on Oct 16, 2014 at 09:53 UTC
    Thanks! But for example:

    use strict; use warnings; use Data::Dumper; open (FILE, "/Users/Pabli/Desktop/gene_association.goa_human"); open (FILE2, "/Users/Pabli/Desktop/go3.obo"); my %go_accession_hash=(); while(<FILE>) { my @array_with_all_fields=split(/\t/); if ($array_with_all_fields[2] eq "TP53"){ my $mio=$array_with_all_fields[4]; %go_accession_hash=($mio,''); } print "".Dumper(%go_accession_hash)."\n"; my @test2= keys %go_accession_hash; } while(<FILE2>){ print "".$test2[0]."\n"; } close FILE; close FILE2;

    When i print the entire hash table outside from the "if", it prints only the last element (key) of the hash table for "n" times and i don't understand the reason!

    Thanks for help

    Paola
      You are overwriting the hash in each step:
      %go_accession_hash=($mio,'');

      To set a single key-value pair, use the following syntax:

      $go_accession_hash{$mio} = '';
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Are all the entries in the fifth column of my $mio=$array_with_all_fields[4] unique ?

      If the answer is "yes" then your hash can be re-written as has been suggested to you (i.e.  %go_accession_hash{$mio}=' ';). If the answer is "no" then you might want to consider using an advanced data structure that is more appropriate to capture each variation associated with $mio.

      In order to help you understand the problem and solution better It will be more useful to actually show a representative example of how your data look like and what you want to achieve from reading it in.


      A 4 year old monk
        Thanks so so so much for your help! The situation is complicated and i'm new in "Perl"! Then: if i print "array_with_all_fields4" this is my output:

        GO:0000060 GO:0000122 GO:0000979 GO:0001077 GO:0001701 GO:0001756 GO:0001836 GO:0002309 .....

        After i store this output into "mio" for a particular need. After i store "mio" into an hash table as the keys of the hash table. If i print the hash table this is the output:

        $VAR1 = 'GO:0000060'; $VAR2 = ''; $VAR1 = 'GO:0000122'; $VAR2 = ''; $VAR1 = 'GO:0000979'; $VAR2 = ''; $VAR1 = 'GO:0001077'; $VAR2 = ''; $VAR1 = 'GO:0001701'; $VAR2 = ''; .....

        the final step for me is to store keys into an array "test2" and after use this array in another "while" and in another "FILE2".

        I know that is so difficult to explain but if you want i can write to you from my personal mail!

        Thaks a lot Paola
      you're not checking for errors :) autodie

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1104020]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2025-12-14 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (94 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.