You should start your Perl program with these two pragmas:
use warnings; use strict;
They will help you find mistakes in your code, like why $ofound is not visible at the end of the subroutine.
12 my $seq =();
It looks like you are attempting to assign a list to a scalar variable, but that is impossible as there is no such thing as a list in scalar context.
23 $o{$header} = "$seq"; 27 print "$line"; 28 $header = "$line"; 33 $seq .= "$line";
What's wrong with always quoting "$vars"?
35 #Q: How many times is the $search found inside all the hash va +lues?? 37 while (my ($key,$value) = each(%o)) { 38 $hits++ while $value =~ /$search/gi; 39 } 40 41 #Q: How many hash values contain at least one <93>$search<94> +inside of them? 42 #################### 43 #FOREACH LOOP START# 44 #################### 45 foreach my $key (keys %o) { 46 $searchline = $o{$key}; 47 if ($searchline =~ /$search/gi) { 48 $contains ++; 49 } 50 } 51 ################### 52 # FOREACH LOOP END# 53 ###################
These two loops do the same thing. So that code could be simplified to:
my $hits; $hits += () = /$search/gi for values %o; my $contains = $hits;
68 if ($count) { 69 my $ofound = ($hits)/($count); 70 } elsif ($count ==0) { 71 }
my creates a variable that is lexically scoped inside the current block or file so $ofound can only be seen on line 69 of your program. You need to declare it outside of that block:
my $ofound; if ($count) { $ofound = ($hits)/($count); } elsif ($count ==0) { }
Or just return that value:
if ($count) { return $hits / $count; } elsif ($count ==0) { }
In reply to Re: Returning from a subroutine.
by jwkrahn
in thread Returning from a subroutine.
by MaroonBalloon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |