You've got a few issues with your code. You're re-declaring some of your variables with my incorrectly (always put use warnings; and use strict; at the top of your scripts), you're using @array in a global sense when you should be passing it into the subroutine instead, and unless you have good reason not to, you should be putting your use statements at the top of your code as it's far easier to see what the code is using.
With these changes and additions, does it do what you expect? I create the array, send it in as a list of parameters to the sub, perform actions on each element, push the result to a new array, then when done the loop, return the entire new array:
use warnings;
use strict;
use List::Util qw( min max );
my @not_normalized = qw(5 4 9 9 6);
my @normalized = normalizer(@not_normalized);
print "$_\n" for @normalized;
sub normalizer {
my @not_normalized = @_;
my $min_numarray = min @array;
my $max_numarray = max @array;
my $normalized;
my @normalized_list;
foreach my $element (@not_normalized){
my $numdiv = $element - $min_numarray;
my $numden = $max_numarray - $min_numarray;
$normalized = $numdiv / $numden;
push @normalized_list, $normalized;
}
return @normalized_list;
}
Output:
0.2
0
1
1
0.4
update: renamed arrays to have sensible names
-
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.
|