长数据如下图
perl代码如下
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my $usage=<<USAGE;
Usage:
perl $0 inputfile
USAGE
if(@ARGV==0){die $usage};
my $file=$ARGV[0];
my %timeinfo=();
my $reftime=\ %timeinfo;
my $i=0;
open(RF,$file) || die $!;
open(WF,">kuan_data.txt") || die $!;
while(my $line=<RF>){
next if ($.==1);
chomp($line);
my @arr=split('\t',$line);
$reftime -> {$arr[0]} -> {$arr[1]} =$arr[2];
}
for my $j (keys %{$reftime}){
$i=0;
for my $dd (keys %{$reftime -> {$j}}){
$i++;
}
}
for my $year (sort {$a cmp $b} keys %{$reftime}){
my $j=1;
for my $month (sort {$a <=> $b} keys %{$reftime -> {$year}}){
if ($j==1){
print WF $month,"\t";
$j++;
}elsif($j<$i){
print WF $month,"\t";
$j++;
}elsif($j==$i){
print WF $month,"\n";
}else{
next;
}
}
last;
}
for my $year (sort {$a cmp $b} keys %{$reftime}){
my $j=1;
for my $month (sort {$a <=> $b} keys %{$reftime -> {$year}}){
if ($j==1){
print WF $year,"\t",$reftime -> {$year} -> {$month},"\t";
$j++;
}elsif($j>0 && $j < $i){
print WF $reftime -> {$year} -> {$month},"\t";
$j++;
}else{
print WF $reftime -> {$year} -> {$month},"\n";
}
}
}
close(RF);
close(WF);
得到最后的宽数据,如下图