The subject line of your question contains the words "key values", that could have given you a hint that you can solve this problem using a hash :-).

Normally I recommend using Text::CSV_XS on CSV files, but this example data looks simple enough to handle without an extra module (reconsider using the module if your real datafiles should contain things like quoted data or escaped commas):

#!/usr/bin/perl use strict; use warnings; my %result; for my $file ("File_A","File_B","File_C") { open (my $fh,"<",$file) or die "Can't open file $file: $!"; <$fh>; # skip header line while (my $line = <$fh>) { my ($server,$cpua) = (split(",",$line))[0,1]; push @{$result{$server}},$cpua; } close $fh or die "Can't close file $file: $!"; } open (my $nfh,">","Result_File") or die "Can't open result file: $!"; for my $server (sort keys %result) { print $nfh $server.",".join(",",@{$result{$server}})."\n"; } close $nfh or die "Can't close result file: $!";

This assumes that you want the result file to contain the servers listed in alphabetical order. If you want a different ordering, simply construct an array holding the server names in the order you want them and use that for the final loop. For example

my @order = qw(wsompapgtw05 wsomddvfxa01 wsomdavpra03 WSOMQAVPRA05); for my $server (@order) { ...

All dogma is stupid.

In reply to Re: Combining Records From Multiple Files based on Common Key Values by tirwhan
in thread Combining Records From Multiple Files based on Common Key Values by country1

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.