in reply to Adding numeric values while keeping string values
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Adding numeric values while keeping string values
by ack (Deacon) on Aug 17, 2009 at 18:12 UTC | |
by alexm (Chaplain) on Aug 17, 2009 at 19:23 UTC |