The following code assumes that a header line is provided for each data set as shown in the sample data. A hash is built containing the merged data from both files then only those records containing data for all columns is printed.
#!/usr/bin/perl use strict; use warnings; my $data1 = <<DATA1; trip value1 value2 ATG adsad dsf CTG 23432 2342 TTA 24312 144 CTT 452 5fw ATA rff sgsh DATA1 my $data2 = <<DATA2; trip value3 ATG asdas CCG asdadd TTA 24 CAT 45 DATA2 my %data; my @columnNames; open my $in, '<', \$data1; push @columnNames, parseFile (\%data, $in); close $in; open $in, '<', \$data2; push @columnNames, parseFile (\%data, $in); close $in; my $format = (('%-9s ') x (@columnNames + 1)) . "\n"; printf $format, '', @columnNames; for my $key (sort keys %data) { next if keys %{$data{$key}} != @columnNames; printf $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; }
Prints:
value1 value2 value3 ATG adsad dsf asdas TTA 24312 144 24
Note that strictures are used. Always use strictures (use strict; use warnings;). The three parameter version of open is used with lexical file handles.
@{$data{$key}}{@columnNames} and @{$dataRef->{$key}}{@columns} are hash slices - they access a list of hash values. The first case returns the list of values to be printed for a row. The second case is used to assign the list of column values to a record.
Note that parseFile doesn't check to see that data column names for the current file are different than any previous file nor that the key column name (assumed to be the first) is the same. Those are all things that can be fixed if you need them to be.
In reply to Re^2: Hash w/ multiple values + merging
by GrandFather
in thread Hash w/ multiple values + merging
by sophix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |