If you work with Perl for any significant amount of time, you'll find that the hash is frequently the easiest way to do things, and I think once you get comfortable using them your facility with the language will increase dramatically.
In this case, I would suggest using a hash of lists in order to hold your data - a basic intro to nested structures in Perl can be found at perllol. You'll probably also want to read up on references at perlref and perlreftut. Since you are using CSV files, it's also probably a good idea to use Text::CSV to handle your files, to avoid unnecessary headaches. Obviously I have no idea if you are already or not, since you haven't posted any code - that's something we usually like to see.
To give you a basic idea of how to use a hash of lists, I've put together the following code with your example:
#!/usr/bin/perl use strict; use warnings; my %servers; while (<DATA>) { chomp; my @line = split /\,/; my $name = shift @line; if (exists $servers{$name}) { foreach my $index (0..$#line) { $servers{$name}[$index] += $line[$index]; } } else { $servers{$name} = [@line]; } } foreach my $key (keys %servers) { print join(',',$key,@{$servers{$key}}), "\n"; } __DATA__ server1,4,2,2 server1,6,2,2 server1,4,1,1 server2,10,1,2 server2,1,1,1
In reply to Re: Hash/Array help
by kennethk
in thread Hash/Array help
by Urbs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |