Solved

Perl array help

Posted on 2011-03-06
4
327 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
Comment Utility
$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
Comment Utility
my $sum=0;

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

Author Comment

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

Expert Comment

by:wilcoxon
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

11 Experts available now in Live!

Get 1:1 Help Now