Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Perl array help

Posted on 2011-03-06
4
Medium Priority
?
384 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
4 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 2000 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 27

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

972 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