Hello! I am working on a program to familiarize myself with testing modules and subroutines. I have everything working properly except for checking if the inputted value is a numeric value or not, and if it isn't, I need to return "Undef". I was given a black box subroutine to do the work for me, but I don't know how I'd implement it into my program >:. I'll post my whole code, and can anyone show me how to do my checking for whether or not an inpytted value is numeric or not? Here's my code, and beneath the module are the tests I use.
sub Average { my @num = @_; #declare variables my $avg; my $check = @num; if (!@num) { #If there is no inputted value, return 0 return 0; } elsif (AsNumber($_[0]) ){ #foreach list, get the average foreach(@num){ $avg += $_ for @_; return ($avg / scalar @_ ); } } else{ return undef; } } sub Median { my $median; #declare variables my @array = @_; my @array = sort { $a <=> $b } @array; #sort to arrange the n +umbers in order if (!@_) {#If there is no inputted value, return 0 return 0; } elsif (@array % 2){ #if divisible by 2, do this to get median $median = $array[($#array / 2)]; return $median; } else{ #if not, use this to get median $median = $array[int($#array / 2)] + (($array[int($#array / 2) ++ 1] - $array[int($#array / 2)]) / 2); return $median; } } sub Low { my @list = @_; #declare variables and assign values my $check = @list; if (!@list) { return 0;#If there is no inputted value, return 0 } else { @list = sort {$a <=> $b} @list; #sort and find lowest value my $low = $list[0]; #take lowest value and return the value return $low; } } sub High { my @array = @_; my $array = @_; if (!@array) { #If there is no inputted value, return 0 return 0; } elsif (looks_like_number(@array)){ @array = sort { $b <=> $a } @array; #reverse sort to find the +highest number my $high = $array[0]; #grab the highest value and return return $high; } else{ return undef; } } # Utility Subroutines ... # AsNumber( $s ) returns # the number encoded by $s, or # undef if $s is not defined, or # undef if $s is not a number sub AsNumber { my ($s) = @_; return undef unless defined( $s ); my $number; eval { # make conversion failure a fatal error use warnings FATAL => qw(numeric); # try to use $s as a number $number = 0 + $s; }; if ($@) { # exception happened, thus not a number $number = undef; } return $number; } 1; # module end, must return true }
END MODULE -> BEGIN TEST
use strict; use warnings; use diagnostics; use Scalar::Util qw(looks_like_number); use Test::More 'no_plan'; use ListStats; print "========================================\n"; diag( "Tests of Average" ); #AVERAGE IS DONE YEEEEEEEEAH # fix/add more tests for Average ... is( Average( ), 0, "Average empty list" ); is( Average( (2) ), 2, "Average singleton list" ); is( Average( (-7..5) ), -1, "Average list" ); is( Average( ("a") ), undef, "Average non-number in list" ); is( Average( 1, 2, 3, 4), 2.5, "Average numeric string - test o +ne" ); is( Average( 52, "53", 93, 2678), 719, "Average numeric string +- test two" ); print "========================================\n"; diag( "Tests of Median" ); is( Median( ), 0, "Median empty list" ); is( Median( (2) ), 2, "Median singleton list" ); is( Median( (-7..5) ), -1, "Median list" ); is( Median( ("a") ), undef, "Median non-number in list" ); is( Median( 0, "1", 2 ), 1, "Median numeric string - test one" ) +; is( Median( 0, "65", 34 ), 34, "Median numeric string - test two +" ); #find length of string, get middle. print "========================================\n"; diag( "Tests of Low" ); # fix/add more tests for Low ... is( Low( ), 0, "Low empty list" ); is( Low( (2) ), 2, "Low singleton list" ); is( Low( (0,2,-2,-1) ), -2, "Low list" ); is( Low( ("a") ), undef, "Low non-number in list" ); is( Low( 0, "-1", 2 ), -1, "Low numeric string - test one" ); is( Low( 34, "-1", -47 ), -47, "Low numeric string - test two" + ); print "========================================\n"; diag( "Tests of High" ); is( High( ), 0, "High empty list" ); is( High( (2) ), 2, "High singleton list" ); is( High( (0,2,-2,-1) ), 2, "High list" ); is( High( ("a") ), undef, "High non-number in list" ); is( High( 0, "-1", 2 ), 2, "High numeric string - test one" ); is( High( 0, "-1", 2,47, 2737 ), 2737, "High numeric string - +test two" );
As you can see I'm pretty new to perl =P. @_ will be tests I input through the main perl file. The above code is part of my module. I need to make it so if @_ is not a numeric value, it returns undef, and not "a" as that is what is expected in my test file. Any help would be extremely appreciated! Thanks in advance! Also, any tips to make my program more "elegant" would be welcome too ^^ Thanks!

In reply to How to implement numeric checking in my program. by Tails

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.