Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: returnong array in custom subroutine

by perldigious (Priest)
on Aug 31, 2016 at 13:54 UTC ( [id://1170891]=note: print w/replies, xml ) Need Help??


in reply to returning array in custom subroutine

use strict; use warnings; my @not_normalized = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); my @normalized = normalizer(@not_normalized); print "@normalized"; sub normalizer { # to normalize an input array bw 0 and 1 my @array = @_; use List::Util qw( min max ); # import min and max module my $min_numarray = min @array; # get min of args my $max_numarray = max @array; # get max of args my $numden = $max_numarray - $min_numarray; # denominator: max tot +al - min total foreach my $index (0..$#array) { # loop over args my $numdiv = $array[$index] - $min_numarray; # numerator: n + - min total $array[$index] = $numdiv / $numden; # normalize } return @array; }

I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious

Replies are listed 'Best First'.
Re^2: returnong array in custom subroutine
by perldigious (Priest) on Aug 31, 2016 at 14:05 UTC

    While the code I give here is just yours with minimum modifications to do what you want, I actually first attempted to find a CPAN module with a "normalize this array of numbers" sort of function built in. I couldn't find one, but perhaps I'm just bad at searching, because it seems like that's something that probably exists...

    I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
    I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious

      When I tried "statistics normalize" this came right up.

      Celebrate Intellectual Diversity

        You win the prize sir! My ability to find modules leaves a lot to be desired, but I just knew something this common must exist. That said, I have always loathed object oriented code (I'm still bad at that too).

        use strict; use warnings; use Normalize; my @not_normalized = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); my @are_normalized = @not_normalized; my $norm = Normalize->new(); $norm->normalize_to_max(\@are_normalized); printf "%.2f ", $_ foreach (@not_normalized); print "\n@are_normalized\n";

        UPDATE: I do get the module to come up right away of course when I search CPAN for "normalize", but I originally tried Googling "cpan normalize array" and that doesn't seem to get anywhere close. Also, I still don't understand for the life of me why I need to call the constructor for a new object just so I can use a method of that object on the array like I do here. Certainly I'm just ignorant when it comes to object oriented code, there must be a way to use the method without actually having to create an object, or to otherwise use an "anonymous" object method call if that's even the right terminology. Did I mention I loathe (a.k.a. need to learn better) object oriented code? :-)

        I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
        I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious
      Thanks! Actually before (attempting to) write my own subroutine, I also tried to google that and I did not find any hits. Maybe we're both bad at googling. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1170891]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-20 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found