0

I am writing a perl script and currently working on a subroutine to sum all values of an array. Currently, my code only reads in each line and stores the entire line into each array element. I need each individual number stored in it's own element.

Here's a sample of my data:

50 71 55 93 115
45 76 49 88 102
59 78 53 96 145
33 65 39 82 100
54 77 56 98 158

Here's my code:

my @array;

 #bring in each line and store into array called 'array'
 open(my $fh, "<", "score")
     or die "Failed to open file: $!\n";
 while(<$fh>) { 
     chomp; 
     push @array, $_;
 } 
 close $fh;

When I call my subroutine to sum the values of the array, my result is 241. That is the sum of each of the first numbers in each line.

Any help or suggestions?

1
  • Where are you calling your subroutine? Commented Apr 27, 2016 at 6:27

1 Answer 1

4

So, you want to add all values inside an array. Easy, But In your code, you are adding strings of values instead of value itself.

With push @array, $_; you are creating an array of lines in the file score.

Try:

print Dumper(\@array);

You will see output like this:

$VAR1 = [
          '50 71 55 93 115',
          '45 76 49 88 102',
          '59 78 53 96 145',
          '33 65 39 82 100',
          '54 77 56 98 158'
        ];

So when you are adding the values, it adds all elements of array:

'50 71 55 93 115' + '59 78 53 96 145' + '33 65 39 82 100' ......and so on

The moment you put + with string it is treated as numeric and by default, perl adds first character in the string to the first character in the other string. If the first character is not a number, It is treated as 0. You should check perlop for more info.

The solution for this problem is to separate the numbers from every line, treat each of them individually and store them inside the array. This can be done simply using:

push @array, split;

Now when you try:

print Dumper(\@array);

It will be like this:

$VAR1 = [
          '50',
          '71',
          '55',
          '93',
          '115',
          '45',
          '76',
          '49',
          '88',
          '102',
          '59',
          '78',
          '53',
          '96',
          '145',
          '33',
          '65',
          '39',
          '82',
          '100',
          '54',
          '77',
          '56',
          '98',
          '158'
        ];

After this just call your subroutine using:

my $total_sum = sum(@array);
print $total_sum,"\n";

and define your subroutine as:

sub sum {
    my @nums = @_;
    my  $total_sum = 0;
    $total_sum += $_ foreach(@nums);
    return $total_sum;
}

The output will be 1937 as expected.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thank you. That is extremely helpful and I appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.