Solved

Perl array help

Posted on 2011-03-06
4
338 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now