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

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?

  • Comment on Re^2: Finding sum of numbers and storing it an Array

Replies are listed 'Best First'.
Re^3: Finding sum of numbers and storing it an Array
by tobyink (Canon) on Sep 21, 2020 at 11:03 UTC

    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.
Re^3: Finding sum of numbers and storing it an Array
by hippo (Archbishop) on Sep 21, 2020 at 09:45 UTC

    You can replace the map with a simple for loop.

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

    🦛

      Thank you so much!

Re^3: Finding sum of numbers and storing it an Array
by choroba (Cardinal) on Sep 21, 2020 at 10:10 UTC
    > 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]