AWallBuilder has asked for the wisdom of the Perl Monks concerning the following question:
I want to perform a seemingly simple task, but am confused. I would like to initialize all values in my hash to equal 60.
my $maxBits=60; # hash reference - gave error message below my %maxBits=60; # wrong %maxBits=map {$_=>60} ; #?
Can't use string ("60") as a HASH ref while "strict refs" in use at fl +agginscript_Flag1.pl line 58, <IN> line 1.
here is a larger chunk of the code so you can see the context
############### read in blast table and parse ####### my $in_blast_tab=$ARGV[0]; open(IN,$in_blast_tab) or die "cannot open $in_blast_tab\n"; my $HoFlg1={}; my $HoFlg2={}; my $maxBits=60; #%maxBits=map {$_=>60} ; # or intialize all values to be 60 using map print "maxBits after intialization\n"; print Dumper($maxBits); while (my $line=<IN>) { next if ($line =~ /^#/); next unless ($line =~ /\S/); chomp $line; my ($Query_id,$strand,$Subj_id,$Perc_iden,$align_len,$num_mm,$ +gap,$q_start,$q_end,$s_start,$s_end,$e_value,$bit_score)=split("\t",$ +line); # extra field of strand next if ($bit_score <60); if ($bit_score > $maxBits->{$Query_id}){ $maxBits->{$Query_id}=$bit_score; } my ($Flag1, $Flag2 ) = &Flag( $Subj_id, \%proph_prots, \%euk_p +rots, \%vir_prots ); $HoFlg1->{$Query_id}->{$Flag1}->{$bit_score}++; $HoFlg2->{$Query_id}->{$Flag2}->{$bit_score}++; print join("\t",$Query_id,$Subj_id,"bit",$bit_score,"F1",$Flag +1,"F2",$Flag2,"maxBits{Query}",$maxBits->{$Query_id})."\n"; } #print "Hash HoFlg1\n"; #print Dumper($HoFlg1)."\n"; #print "Hash maxBits\n"; #print Dumper(%maxBits)."\n"; sub Flag { my ( $Subj_id, $proph_prots, $euk_prots, $vir_prots ) = @_; return "Proph", "Phage" if exists $$proph_prots{$Subj_id}; return "Euk", "Euk" if exists $$euk_prots{$Subj_id}; return "Vir", "Phage" if exists $$vir_prots{$Subj_id}; return "Bact", "Bact"; } ## end sub Flag my $bits_perc=$ARGV[1]; # consider hits within this percent of top hit my $outfile="$in_blast_tab.$bits_perc.FlagTab"; open(OUT,">",$outfile); print OUT "# this file was generate by $0 on".localtime(time)."\n"; print OUT "# these are the counts of hits for each Flag that are withi +n $bits_perc of the top bit_score\n"; print OUT "#".join("\t",qw(Query count_Proph count_Euk count_Vir count +_Bact))."\n"; my @flag_list2=("Phage","Euk","Bact"); my @flag_list1=("Proph","Euk","Vir","Bact"); foreach my $q (keys %{$HoFlg1}){ print OUT "$q\t"; for my $flag (@flag_list1){ my $count={}; if (! exists $HoFlg1->{$q}->{$flag}){ $count->{$flag}=0; } else { for my $b (keys %{$HoFlg1->{$q}->{$flag}}){ if ($b > $bits_perc*$maxBits->{$q}){ $count->{$flag} += $HoFlg1->{$q}->{$flag}->{$b +}; } } } if (! defined $count->{$flag}){ $count->{$flag}=0; } print OUT "$count->{$flag}\t"; } print OUT "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: initialize all values in a hash
by DrHyde (Prior) on May 18, 2012 at 10:35 UTC | |
|
Re: initialize all values in a hash
by tobyink (Canon) on May 18, 2012 at 10:44 UTC | |
by tobyink (Canon) on May 18, 2012 at 13:58 UTC | |
by DrHyde (Prior) on May 18, 2012 at 14:03 UTC | |
by tobyink (Canon) on May 18, 2012 at 14:27 UTC | |
by DrHyde (Prior) on May 18, 2012 at 15:35 UTC | |
| |
by AWallBuilder (Beadle) on May 18, 2012 at 11:02 UTC | |
|
Re: initialize all values in a hash
by Anonymous Monk on May 18, 2012 at 10:33 UTC | |
|
Re: initialize all values in a hash
by locked_user sundialsvc4 (Abbot) on May 18, 2012 at 14:46 UTC |