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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.