in reply to Returning from a subroutine.

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) { }

Replies are listed 'Best First'.
Re^2: Returning from a subroutine.
by AnomalousMonk (Archbishop) on Dec 13, 2009 at 11:44 UTC
    35 #Q: How many times is the $search found inside all the hash values? +? ... 41 #Q: How many hash values contain at least one "$search" inside of t +hem? ...
    These two loops do the same thing. ...

    But 'how many times is X found in all hash values?' and 'how many values contain at least one X?' are two different questions. A hash might have many values, but only one or two of those values might have an X. However, the total number of Xs in those few values might be very many.

    >perl -wMstrict -le "my %hash = ( x => '---------', y => '-----x---', z => '-XxX-x-X-', ); my $Xs_in_values = 0; $Xs_in_values += () = /X/gi for values %hash; print $Xs_in_values; my $values_with_X = grep /X/i, values %hash; print $values_with_X; " 6 2

    Update: The code of the OP for answering these two questions seems correct, although a bit klunky.

Re^2: Returning from a subroutine.
by JavaFan (Canon) on Dec 13, 2009 at 12:32 UTC
    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.
    EWRONGDOGMA

    Had the OP not used strict, and not use my, he wouldn't have had the problem. His problem is the overuse of my, as you point out. Adding either use warning; or use strict; does diddly-squat to solve the problem.