in reply to Combining Records From Multiple Files based on Common Key Values
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) { ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Combining Records From Multiple Files based on Common Key Values
by country1 (Acolyte) on Jul 16, 2007 at 12:39 UTC | |
by tirwhan (Abbot) on Jul 16, 2007 at 18:17 UTC | |
by country1 (Acolyte) on Jul 17, 2007 at 15:45 UTC | |
by tirwhan (Abbot) on Jul 17, 2007 at 16:53 UTC | |
by country1 (Acolyte) on Jul 17, 2007 at 18:28 UTC | |
|