Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Problem of context?

by pabla23 (Novice)
on Oct 16, 2014 at 08:20 UTC ( [id://1104018]=perlquestion: print w/replies, xml ) Need Help??

pabla23 has asked for the wisdom of the Perl Monks concerning the following question:

Good Morning! I have this problem, i hope you can help me:

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

When the compiler enter into the second while the variable "test2" is not seen! I try to "play"with the declaration global/local...but the result is the same! Can someone help me?

Thanks Paola

Replies are listed 'Best First'.
Re: Problem of context?
by hippo (Bishop) on Oct 16, 2014 at 08:37 UTC

    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.

      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
        you're not checking for errors :) autodie
Re: Problem of context?
by roboticus (Chancellor) on Oct 16, 2014 at 13:53 UTC

    pabla23:

    Not only would variable test2 not be seen, the program wouldn't even compile. Try posting the code you actually run or don't gratuitously paste in a "use strict; use warnings;" at the beginning if you're not actually using them.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1104018]
Approved by GrandFather
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2024-04-24 17:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found