As other Monks have noted, the problem is that in some instances of $servers{$name}[$index] += $line[$index] you're trying to do numeric addition on non-numeric, string, data.

JavaFan suggested using the CPAN module Scalar::Util's function looks_like_number or you can use Scalar::Util::Numeric's function isnum($val) where the $val is the scalar that you're checking to see if it's a number.

I have coded up a quick example using the Scalar::Util::Numeric as an example for you to see how it might be used to solve the problem you alluded to in your inquiry.

My example codes is as follows:

#!/user/bin/perl use strict; use warnings; use Scalar::Util::Numeric qw(:all); my %servers = ( 'cihcrppmon02'=>['standard_unix_corp-cis_shared',170399 +5999752,133635610485,31055995112,12.75,54.8,'ghnbu','gh_nbu02'], 'ovpip02' =>['standard_unix_corp-cis_shared',418510 +38572,14933570508,3835386732,2.80,10.91,'ghnbu','gh_nbu02'], ); my @input = ( "cihcrppmon02,standard_unix_corp-cis_shared,1703995999752,133635610 +485,31055995112,12.75,54.8, ghnbu, gh_nbu02\n", "ovpip02,standard_unix_corp-cis_shared,41851038572,14933570508,3835 +386732,2.80,10.91,ghnbu, gh_nbu02\n", "cihcispapp247,standard_unix_corp-cis_shared,190635017908,248064076 +11,9442867181,7.68,20.19,ghnbu, gh_nbu02\n", "wavmd003,standard_unix_corp-cis_shared,2760141244,594115253,299382 +152,4.65,9.22,ghnbu, gh_nbu02\n", ); foreach my $line_in (@input){ chomp($line_in); my @line = split(/,/,$line_in); my $name = shift @line; if(exists $servers{$name}){ foreach my $index (0..$#line){ if(isnum($line[$index]) && isnum($servers{$name}[$index])){ $servers{$name}[$index] += $line[$index]; } } } else { $servers{$name} = [@line]; } } foreach my $name (keys %servers){ print "$name: " . join(" ",@{$servers{$name}}) . "\n"; } exit(0);

This produces the results:

cihcrppmon02: standard_unix_corp-cis_shared 3407991999504 2672712209 +70 62111990224 25.5 109.6 ghnbu gh_nbu02 ovpip02: standard_unix_corp-cis_shared 83702077144 29867141016 7670 +773464 5.6 21.82 ghnbu gh_nbu02 cihcispapp247: standard_unix_corp-cis_shared 190635017908 2480640761 +1 9442867181 7.68 20.19 ghnbu gh_nbu02 wavmd003: standard_unix_corp-cis_shared 2760141244 594115253 299382 +152 4.65 9.22 ghnbu gh_nbu02

I had to put your example input into an array to simulate your input stream. In order to show that the numeric part of the example works, I also pre-populated the hash %servers with a couple of entries (just copies of your input example for a couple of the servers) so that the numeric code would be exercised).

I didn't know what you intended to do if various entries for a particular server had different non-numeric data (for example, if for the server 'cihcrppmon02' you had 'ghnbu' in one entry and perhaps something like 'gxfoo' in another). So I just assumed that the non-numeric data in all entries would be the same. This is probably not quite what you had in mind (perhaps you would want to append the added information or something like that); but you didn't give any indication what you wanted to do with such data.

I hope this helps to show at least one strategy for you.

ack Albuquerque, NM

In reply to Re: Adding numeric values while keeping string values by ack
in thread Adding numeric values while keeping string values by Urbs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.