Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl array help

Posted on 2011-03-06
4
Medium Priority
?
376 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 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 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

721 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