将数据从一列重新排列到一行

时间:2014-10-18 18:47:16

标签: perl

我有以下数据,我需要将第二列作为标题。任何帮助表示赞赏。

数据:

IBM,Voltality,7,73894756.93897434897
IBM,Market,100,983874.34324
GOOG,Sanity,15,8932748
GOOG,Rate Jump,25,873476378.234234
MBLY,Market,340,23423423432.6783

输出:

PRODUCT|Market|Rate Jump|Sanity|Voltality
IBM|100,983874.34324|||7,73894756.93897434897
GOOG||25,873476378.234234|15,8932748|||
MBLY|340,23423423432.6783|||

代码(不完整/不确定热到底):

#!/usr/bin/perl
use strict;
use Getopt::Long;
use warnings;
use Data::Dumper;

my $valsep = ',';

my ( %type, %keys, %ccy, %cnt, %avg );
while (<>) {
    chomp;
    my ( $product, $reason, $count, $lat ) = split /,/;
    my $key = "$product,$reason";

    if ( not exists( $type{$reason} ) ) {
        $type{$reason} = $reason;
    }
    $ccy{$key} = $product;
    $cnt{$key} = $count;
    $avg{$key} = $lat;

}

close(INPUT);

print Dumper ( \%ccy );
print Dumper ( \%type );

my ( %pair, %details );

foreach my $rows ( sort keys %ccy ) {
    print "the key is : $rows and $ccy{$rows}\n";
    foreach my $res ( sort keys %type ) {
        print "The type is : $res and $type{$res}\n";

    }

}

2 个答案:

答案 0 :(得分:0)

以下代码将完成这项工作;我没有使用几个哈希值,而是将所有数据放入散列哈希值。我已在脚本中添加注释,以解释在您不确定的情况下发生的情况。当然,您可以在脚本中删除它们。

#!/usr/bin/perl
use warnings;
use strict;

my %market;
while (<DATA>) {
    next unless /\w/;
    # remove line endings
    chomp;
    # split line by commas -- only split into three parts
    my @col = split ",", $_, 3;
    # save the data as $market{col0}{col1} = col2
    $market{$col[0]}{$col[1]} = $col[2];
}

# create an output file
my $outfile = 'output.txt';
open( my $fh, ">", $outfile ) or die "Could not open $outfile: $!";

my @headers = ('Market','Rate Jump','Sanity','Volatility');

# print out the header line, joined by |
print { $fh } join('|', 'PRODUCT', @headers) . "\n";

# for each product in the market data
for my $p (sort keys %market) {
    # print the product name    
    print { $fh } join('|', $p, 
    # go through the headers using map (map acts like a "for" loop)
    # if the relevant property exists in the market data, print it;
    # if not, print nothing
                  map { $market{$p}{$_} // '' } @headers) . "\n";
}

# this is the input data. You might be reading yours in from a file
__DATA__
IBM,Voltality,7,73894756.93897434897
IBM,Market,100,983874.34324
GOOG,Sanity,15,8932748
GOOG,Rate Jump,25,873476378.234234
MBLY,Market,340,23423423432.6783

输出:

PRODUCT|Market|Rate Jump|Sanity|Volatility
GOOG||25,873476378.234234|15,8932748|
IBM|100,983874.34324|||7,73894756.93897434897
MBLY|340,23423423432.6783|||

答案 1 :(得分:0)

在解析数据结构时,您只需要跟踪列和行数据。

以下演示:

#!/usr/bin/perl
use strict;
use warnings;

my $fh = \*DATA;

my %columns;
my %rows;

while (<$fh>) {
    chomp;
    my ( $company, $col, $vals ) = split ',', $_, 3;

    # Track Columns for later labeling
    $columns{$col}++;

    $rows{$company}{$col} = $vals;
}

my @columns = sort keys %columns;

# Header
print join( '|', 'PRODUCT', @columns ), "\n";

for my $company ( sort keys %rows ) {
    print join( '|', $company, map { $_ // '' } @{ $rows{$company} }{@columns} ), "\n";
}

__DATA__
IBM,Voltality,7,73894756.93897434897
IBM,Market,100,983874.34324
GOOG,Sanity,15,8932748
GOOG,Rate Jump,25,873476378.234234
MBLY,Market,340,23423423432.6783

输出:

PRODUCT|Market|Rate Jump|Sanity|Voltality
GOOG||25,873476378.234234|15,8932748|
IBM|100,983874.34324|||7,73894756.93897434897
MBLY|340,23423423432.6783|||