Perl从两列中分组数据

Perl从两列中分组数据,perl,grouping,rows,Perl,Grouping,Rows,我目前有一个perl脚本,它以这种方式将内容附加到一个文件中: a 1 b 2 c 3 a 2 b 3 c 4 a 3 b 4 c 5 我希望输出是这样的: a 1 2 3 b 2 3 4 c 3 4 5 这是我的代码,它正在从文件中提取2列: open my $file_in, "<", "input_file.txt" or die($!); open my $file_out, '>', 'output_file.txt' or die $!; $now_str

我目前有一个perl脚本,它以这种方式将内容附加到一个文件中:

a 1
b 2
c 3
a 2
b 3
c 4
a 3
b 4
c 5
我希望输出是这样的:

 a 1 2 3
 b 2 3 4
 c 3 4 5
这是我的代码,它正在从文件中提取2列:

open my $file_in, "<", "input_file.txt" or die($!);
open my $file_out,   '>', 'output_file.txt' or die $!;

$now_string = localtime;
print "$now_string\n";
while( <$file_in> ) {
    next unless $i++;
    my @columns = split /\s+/, $_;
    print $file_out "$now_string\t$columns[0]\t$columns[2]\n";

}
close $file_in;
close $file_out or die $!;

在“中打开我的$file_”很简单,但它有助于了解到目前为止如何获得数据-变量

你需要一个HashArray。类似于:

$x->{'a'} ||= []; # init array
push $x->{'a'}, 1; # add value


我没有运行代码,只是在这里对其进行了黑客攻击。

也许您需要
列表::UtilsBy
中的
分区

use 5.010;
use List::UtilsBy 'partition_by';

my @pairs = (
  [ 'a', 1 ],
  [ 'b', 2 ],
  ...
);

my %groups = partition_by { $_->[0] } @pairs;

foreach my $group ( sort keys %groups ) {
  say "Group $group: " . join " ", @{$groups{$group}};
}
更冗长(和近似)的版本

调用打印时追加换行符 我的%h; #从标准文本中读取行 而(){ #空格上的分割线 我的@F=分裂; #将第二列的值推送到$h{$F[0]}数组中 推送{$h{$F[0]},$F[1]; } #对第一列的唯一值进行排序 对于我的$k(排序键%h){ #打印第一列的值, #以及属于它的价值观 打印连接“,$k,@{$h{$k}; }
产出:

a 1 2 3
b 2 3 4
c 3 4 5

你至少应该把你到目前为止尝试过的东西放进去,而不仅仅是问题。你可以很容易地用散列来做

use Data::Dumper;
use autodie;

# Parse text file
open(FH,$ARGV[0]);
my @array=<FH>;
my %hash;

foreach(@array)
{
    $_=~m/(\w+)\s(\w+)/;
    push(@{$hash{$1}},$2);
}

print Dumper \%hash;
使用数据::转储程序;
使用自动模具;
#解析文本文件
开放式(FH,$ARGV[0]);
my@array=;
我的%hash;
foreach(@array)
{
$uz=~m/(\w+)\s(\w+)/;
push(@{$hash{$1}},$2);
}
打印转储程序\%hash;

你自己试试看,告诉我们你试过什么,我们可以从那里开始。第一眼:
parse.pl
$perl parse.pl input.txt
。你赢了!我使用的是Solaris 9机器,所以不能运行perl模块或类似的perl-E、perl-lane。我使用的是Solaris 9机器,所以不能运行perl模块或类似的perl-E、perl-lan我使用的是Solaris 9机器,因此无法运行perl模块或类似的perl-E、perl-lane为什么Solaris 9不允许“perl模块”"? 如果你能把自己的代码放在上面,你就可以安装模块了。即使在$HOME中,也可以在必要时与您的应用程序一起使用。我使用的是Solaris 9机器,因此无法运行perl模块或类似的perl-E,perl-lanev5.6.1,为sun4-Solaris-64Int构建。当然可以,但我遇到了错误“v:Undefined variable”。请您解释一下这三行代码到底在做什么?谢谢
$\ = "\n";
push @{$h{$_->[0]}}, $_->[1] for map [split], <>;
print join " ", $_, @{$h{$_}} for sort keys %h;
$\ = "\n"; # append newline when invoking print

my %h;
# read lines from STDIN
while (<>) {
  # split line on white spaces
  my @F = split;
  # push value of second column into $h{$F[0]} array
  push @{ $h{$F[0]} }, $F[1];
}

# sort unique values of first column
for my $k (sort keys %h) {

  # print value of first column, 
  # together with values belonging to it
  print join " ", $k, @{ $h{$k} };
}
perl -lane "
  push @{$v{shift @F}}, @F; 
  END { print qq{$_ @{$v{$_}}} for sort keys %v }
" file1.txt
a 1 2 3
b 2 3 4
c 3 4 5
use Data::Dumper;
use autodie;

# Parse text file
open(FH,$ARGV[0]);
my @array=<FH>;
my %hash;

foreach(@array)
{
    $_=~m/(\w+)\s(\w+)/;
    push(@{$hash{$1}},$2);
}

print Dumper \%hash;