Solved

Perl array help

Posted on 2011-03-06
4
359 Views
Last Modified: 2012-05-11
I need the sum of an array elements however there are dashes "-" that cause errors:

$VAR1 = [
          '298',
          '1922',
          '-',
          '1059',
          '5118',
          '-',
          '1485',
          '1302',
          '1043',
          '-',
          '48615',
                                       
Which causes these errors:

Argument "-" isn't numeric in addition (+) at ./stat23 line 197, <LOG> line 101.
Argument "-" isn't numeric in addition (+) at ./stat23 line 197, <LOG> line 101.
Argument "-" isn't numeric in addition (+) at ./stat23 line 197, <LOG> line 101.
Argument "-" isn't numeric in addition (+) at ./stat23 line 197, <LOG> line 101.
Total bytes: 245330

I have tried split & search/replace with no luck.
The values move around and cause incorrect sum.
Thanks in advance.
0
Comment
Question by:fac66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 35053530
$VAR1 = [
          '298',
          '1922',
          '-',
          '1059',
          '5118',
          '-',
          '1485',
          '1302',
          '1043',
          '-',
          '48615'];

print "before...\n";
print join("\n",@{$VAR1});

# this line fixes the dashes
foreach $x (@{$VAR1}) { if ( $x eq '-' ) { $x = 0; } }

print "\n\nafter...\n";
print join("\n",@{$VAR1});

Open in new window

0
 
LVL 31

Expert Comment

by:farzanj
ID: 35053561
my $sum=0;

foreach $x (@{$VAR1})
{
      if ( $x ne '-' )
      {
              $sum = $sum + $x;
      }
}
0
 

Author Comment

by:fac66
ID: 35053643
Thanks guys..
I tried the first one and worked perfect.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35058526
A couple of alternatives...

use List::Util qw(sum);
my $sum = sum(map { /^\d+$/ ? $_ : 0 } @$VAR1);

-or-

my $sum;
foreach my $x (@$VAR1) { $sum += $x if ($x =~ m{^\d+$}); }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl script 4 115
Perl for loop for 2000 ms 7 109
Port 80 requests 16 102
Why would it print (?si-xm:my.STRING) instead of "my.STRING"  and what does "?si-xm:" mean. 7 93
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question