Hayest has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm trying to get the sum of the elements inside of my array. The elements inside of my array are 5 random numbers between 1 and 50. This is what I've got so far:

#!/usr/bin/perl use strict; use warnings; my %num; while ((keys %num) < 5) { $num{int(rand(50)+1)} = 1; } print join(' ' , keys %num) . "\n"; my @array = join(' ' , keys %num) . "\n"; my $sum = 0; foreach my $var (@array) { $sum = $sum + $var; } print("The sum is: $sum\n");

This first gives me my 5 random numbers, then, the line below is supposed to say "The sum is: (my sum here)." I keep getting the following as an error, and only the first random number is displayed:

sh-4.2# perl
main.pl
16 30 21 7 11
Argument "16 30 21 7 11\n" isn't numeric in addition (+) at main.pl line 18.
The sum is: 16

Does anyone have any input on what I can do to make this work correctly? Thank you!!!

Replies are listed 'Best First'.
Re: Trying to get sum of elements in array
by toolic (Bishop) on Feb 05, 2015 at 18:41 UTC
      Thank you! That works perfectly. I need to look into use Data::Dumper, also.
      I wonder what other homework he has for us...
        Yes, it is homework. Is it an issue that I'm asking for help?
Re: Trying to get sum of elements in array (map)
by tye (Sage) on Feb 05, 2015 at 22:49 UTC
    my $sum = () = map { (1)x$_ } @array;

    Of course. ;-p

    - tye        

      Or use a module.
      use strict; use warnings; use List::Util qw(sum); print 'The sum is : ', sum(map {rand 50} 1..5), "\n";
      Bill
      or the evil evil way to do it... ;=)
      perl -e " print eval join '+',@ARGV " 16 30 21 7 11 85


      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Trying to get sum of elements in array
by gargle (Chaplain) on Feb 06, 2015 at 12:05 UTC
    About filling up your array... you're using a random index number and will only stop if there are 5 different indexes, possibly looping more than 5 times. I find the following much more clean: It only loops 5 times, for sure:
    my @list = (); foreach my $i (1..5) { push @list, int(rand(50)+1); }
    --
    if ( 1 ) { $postman->ring() for (1..2); }
      But the result isn't guaranteed to be unique.

      If you worry about performance rather use a shuffle approach.

      Or use the fact that the order of keys in a hash is random.

      Cheers Rolf

      PS: Je suis Charlie!

        Why do these random numbers have to be unique?
        --
        if ( 1 ) { $postman->ring() for (1..2); }
Re: Trying to get sum of elements in array
by igoryonya (Pilgrim) on Feb 07, 2015 at 16:54 UTC
    There are several problems in your code:
    my @array = join(' ' , keys %num) . "\n";
    You apply a string value (scalar) to an array. It looks like you are trying to print it. You probably want to do this:
    my @array = keys %num;
    There is a simpler way to do realize your program:
    my @num = ();
    It's easier to use an array, instead of hash for your program.
    while(scalar @num < 5){ push @num, int(rand(50) + 1); }
    or, even easier:
    push(@num, int(rand(50) + 1)) for(1..5);
    print join(' ' , @num), "\n";
    my $sum = 0; for(@num){ $sum += $_; } print "The sum is: $sum\n";
    Or, the final code:
    my @num = (); push(@num, int(rand(50) + 1)) for(1..5); my $sum = 0; for(@num){ $sum += $_; } print join(' ' , @num), "\n"; print "The sum is: $sum\n";
    Or, group the loops:
    my @num = (); my $sum = 0; for(1..5){ my $num = int(rand(50) + 1); push(@num, $num); $sum += $num; } print join(' ' , @num), "\nThe sum is: $sum\n";