2014-10-18 47 views
-1

我有以下數據,我需要將第二列作爲標題。任何幫助表示讚賞。重新排列數據從一列到一行

數據:

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"; 

    } 

} 
+0

你可能忘記從你的第一個試驗提到你的代碼來解決這個 – 2014-10-18 19:33:18

+0

我就不能完成。以下是我到目前爲止所得到的結果。 – Tinman 2014-10-18 19:40:24

回答

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||| 
+0

我寫了很多這樣的東西,但是我浪費了十幾條線獲得正確順序的列。我猜他們是按照第一次出現在行中的順序,向後讀。我沒有注意到簡單的阿爾法排序會產生相同的結果! – Borodin 2014-10-18 22:03:35

+0

哈哈!當然,我的alpha排序只是簡單的懶惰,並希望輸出是一致的。我沒有意識到這是OP所要求的確切順序,直到你指出。 =) – Miller 2014-10-18 22:06:44

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||| 
+0

@Borodin評論是OP,誰是Perl的新手。我在代碼中提出了很多意見,希望OP能夠閱讀並試圖理解它,而不是僅僅寫一些OP會剪切粘貼的東西,並且什麼也不學。 – 2014-10-18 22:09:26