in reply to Finding sum of numbers and storing it an Array

It's an okay way to do it if you understand how it works. For future maintenance, it's better to have code that you understand than code you don't understand.

But if in the future, you ended up having 20 or 30 numbers instead of five, the code will become pretty nasty. You'll likely get random errors in your results, only to realize after hours of investigation that you included $num17/$totalSum in the array, but forgot to include +$num17 when adding the numbers together. Or something like that.

For that reason, I'd recommend using an array to keep your numbers in.

#!/usr/bin/env perl use strict; use warnings; my @nums = ( 5, 10, 15, 20, 25 ); # Loop through the array to add each number, one at a time to make the + total. my $totalSum = 0; for my $num ( @nums ) { $totalSum += $num; } # Map loops through a list, applying a block to each item, to create a + new list my @array = map { $_ / $totalSum } @nums; print "The required numbers are: @array";

Because adding a list of numbers together is a pretty common thing to do, there's a function called sum that comes with Perl to do it:

#!/usr/bin/env perl use strict; use warnings; use List::Util 'sum'; my @nums = ( 5, 10, 15, 20, 25 ); my $totalSum = sum @nums; my @array = map { $_ / $totalSum } @nums; print "The required numbers are: @array";

You probably don't need to worry about whether the user will have List::Util installed. It has been bundled with Perl since 2002.

Replies are listed 'Best First'.
Re^2: Finding sum of numbers and storing it an Array
by shabird (Sexton) on Sep 21, 2020 at 09:32 UTC

    the thing is i am not allowed to use maps in this program only array i can use so in this situation is my approach ok? or can it become better using only arrays?

      the thing is i am not allowed to use maps in this program only array

      To be clear, in some programming languages, there's a data structure called a "Map". Java is one such language. It stores a mapping from one set of data (the keys) to another set of data (the values). Perhaps domain names to IP addresses:

      ( "foo.example.com" => "10.0.0.1", "bar.example.com" => "10.0.0.2", "baz.example.com" => "10.0.0.3", )

      Perl doesn't have a native map datatype. For some purposes, Perl hashes can be used. But if the keys can't be reduced to strings, Perl hashes become trickier. (Tied hashes and fieldhashes can work around this limitation to an extent.)

      The Perl map keyword doesn't have anything to do with Map data structures though. It operates on lists. (And an array is basically just a way of storing a list in a variable.)

      Anything you do with map and grep, you can do with plain old loops, but map and grep are often clearer ways to express yourself.

      A way to think of it might be: can you speak/write using only one-syllable words?

      • Yes, you can say all the things you want to say with the use of words that have just one sound part in them.
      • But polysyllabic words sometimes allow for more concise expression.

      You can replace the map with a simple for loop.

      my @array; push @array, $_ / $totalSum for @nums;

      🦛

        Thank you so much!

      > i am not allowed to use maps

      Is it a homework? Or a workplace optimising for newbies? Or maybe some kind of cartography elimination?

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]