Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Problem in a Loop

by madM (Beadle)
on Sep 19, 2013 at 12:50 UTC ( [id://1054845]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks! I have this loop but if i run it i become in the hash reference $freqocurrence a lot of 0s and i donīt know why.. any suggestions?
my $freqocurrence={}; my $observation={}; my $total10=0; sub freq_oc{ foreach my $key (%$observation){ $total10 += $observation->{$key}; } foreach my $key (%$observation){ $freqocurrence->{$key} = $observation->{$key}/$total10; print "Frequency of ocurrence:Aminoacid $key-> $freqocurrence->{$k +ey}\n"; } print Dumper $observation; print Dumper $freqocurrence; }

Replies are listed 'Best First'.
Re: Problem in a Loop
by hdb (Monsignor) on Sep 19, 2013 at 13:01 UTC

    If you want to loop over the keys of the hashes, you have to say so:

    foreach my $key (keys %$observation){ foreach my $key (keys %$observation){
      now it works but i have another question .. do you know why when i print data Dumper i donīt get anything?
      my $freqocurrence={}; sub freq_oc{ foreach my $key (keys %$observation){ $total10 += $observation->{$key}; } foreach my $key (keys %$observation){ $freqocurrence->{$key} = $observation->{$key}/$total10; print "Frequency of ocurrence: Aminoacid $key-> $freqocurrence +->{$key}\n"; } } print Dumper $freqocurrence;

        I can only guess that your script misses the following three lines near the top:

        use strict; use warnings; use Data::Dumper;
      thanks! i completely forgott it! :D
Re: Problem in a Loop
by derby (Abbot) on Sep 19, 2013 at 13:25 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 14:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found