in reply to Re^4: Hash w/ multiple values + merging
in thread Hash w/ multiple values + merging
#!/usr/bin/perl use strict; use warnings; my $data1 = "/PRBB/input.txt"; my $data2 = "/PRBB/input2.txt"; my $data3 = "/PRBB/output.txt"; my %data; my @columnNames; #open my $in, '<', \$data1; open my $in, '<', $data1 or die "Unable to open $data1: $!"; push @columnNames, parseFile (\%data, $in); close $in; #open $in, '<', \$data2; open my $in2, '<', $data2 or die "Unable to open $data2: $!"; push @columnNames, parseFile (\%data, $in2); close $in2; my $format = (('%-9s ') x (@columnNames + 1)) . "\n"; open my $out, '>', $data3 or die "Unable to open $data3: $!"; for my $key (sort keys %data) { next if keys %{$data{$key}} != @columnNames; printf $out $format, $key, @{$data{$key}}{@columnNames}; } sub parseFile { my ($dataRef, $inFile) = @_; my $header = <$inFile>; my ($keyColumn, @columns) = map {chomp; split} $header; while (defined (my $line = <$inFile>)) { chomp $line; my ($key, @data) = split /\s+/, $line; @{$dataRef->{$key}}{@columns} = @data; } return @columns; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Hash w/ multiple values + merging
by GrandFather (Saint) on Feb 08, 2010 at 01:17 UTC | |
by sophix (Sexton) on Feb 08, 2010 at 01:40 UTC | |
by sophix (Sexton) on Feb 08, 2010 at 02:52 UTC |