sub isprime($) {
my $num = $_[1];
$primer = 1;
$ValidChars = "2357";
$str = $num;
if (index($ValidChars, ord(substr $str, 0, 1)) == 1) {
$primer = 0;
}
return $primer;
}
That returns 1 unless the first character of the first argument (assuming you fix the already noticed error regarding $_[1]) equals 3. I think you are calling isprime only with single digit numbers, and want to return if the digit is a prime number. I'd write that as:
sub isprime {$_[0] =~ /^[2357]$/}
although I would fix the name. Or just inline the test -- that's a lot clearer than any name I can think of.
if (($n != 1) && ($n != 0)) {
if (isprime($n)) {
$prime += $n;
}
if (!isprime($n)) {
$comp += $n;
}
}
Too many ifs, and too many calls to isprime. I'd write that as:
if ($n =~ /^[2357]$/) {
$prime += $n;
}
elsif ($n !~ /^[01]$/) {
$comp += $n;
}
Or, when I'm in the mood for something funky, as a ternary returning an lvalue.
$text.length
That should be spelled length($text).
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|