Welcome to the Perl community. You might find it helpful to read How do I post a question effectively? to learn a bit more about formatting in the Monastery and what we like to see in a question.

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

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.