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


In reply to Re: returnong array in custom subroutine by stevieb
in thread returning array in custom subroutine by dovah

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.