从制表符分隔的文本文件列制作数组
我想知道是否有人可以用perl来帮助绝望的新手以下问题。我一整天都在努力,但是我的perl书在工作中,我似乎无法在谷歌里找到任何相关的东西......或许我真的很愚蠢。从制表符分隔的文本文件列制作数组
我有一个看起来像一个文件中:
Bob April
Bob April
Bob March
Mary August
Robin December
Robin April
输出文件我后:
Bob April April March
Mary August
Robin December April
因此,它列出了每个月的顺序,它似乎为每个人。
我试着把它变成一个散列,但当然它不会让我有重复,所以我想我想每个名称都有数组(在这个例子中,鲍勃,玛丽和罗宾)。 我害怕上传我一直试图调整的代码,因为我知道这将是可怕的错误。我想我需要定义(?)数组。这是正确的吗?
任何帮助将不胜感激,我保证我将在此期间更多地研究perl。
谢谢你的时间,耐心和帮助。
#!/usr/bin/perl -w
while (<>) {
chomp;
if (defined $old_name) {
$name=$1;
$month=$2;
if ($name eq $old_name) {
$array{$month}++;
}
else {
print "$old_name";
foreach (@array) {
push (@array, $month);
print "\[email protected]";
}
print "\n";
@array=();
$array{$month}++;
}
}
else {
$name=$1;
$month=$2;
$array{month}++;
}
$old_name=$name;
}
print "$old_name";
foreach (@array) {
push (@array, $month);
print "\[email protected]";
}
print "\n";
对于这么简单的任务,你的代码看起来过于复杂。
use strict;
use warnings;
my %hash;
while (<DATA>) {
my ($name, $mon) = split;
push @{$hash{$name}}, $mon;
}
for my $name (keys %hash) {
my @months = @{$hash{$name}};
print "$name\[email protected]\n";
}
__DATA__
Bob April
Bob April
Bob March
Mary August
Robin December
Robin April
一个简单的方法来做到这一点是使用Perl的push和pop功能(因为你开始使用perl的:http://perldoc.perl.org/functions/pop.html,http://perldoc.perl.org/functions/push.html)。 你应该让每个名称的全局阵列(例如@bobmonths)和每次找到一个推送一个月。 完成后,打印出名称和阵列:
while(<>)
{
chomp;
push(@bobmonths, $2)
...
}
print @bobmonths
您有点接近。你确实希望使用一个以名字为关键字的散列,但正如你所看到的,对于你想要存储的每个名字数组,因此你希望使用的数据结构是数组散列(或者更确切地说数组引用,因为这是在Perl中实现的)
虽然在此,请不要养成使用全局变量的习惯 - 您的代码的100%在开始时应该有“use strict; use warnings;
”,并且在本地范围内(my
)变量。
use strict;
my %data;
my @sorted_names; # Only needed if you care which order to print the results
while (<>) {
chomp;
my ($name, $month) = split(/s+/);
if (! $data{$name}) {
# Initialize to empty array reference if first time.
# Not required - perl will do it for you
$data{$name} ||= [];
# Only needed if you want to print results in the same order of names as input.
push @sorted_names, $name;
}
push @{ $data{$name} }, $month;
}
foreach my $name (@sorted_names) {
print "$name\t" . join(" ", @{ $data{$name} }) . "\n";
}
# If don't care about name order, just do "foreach my $name (keys %data) {"
脚本:
#!/usr/bin/perl
use strict;
use warnings;
my %content;
open my $fh, '<file.txt' or die $!;
while (<$fh>) {
push @{$content{$1}}, $2 if /^(\S+)\s+(\S+)\s*$/;
}
close $fh;
foreach (keys %content) {
print $_, "\t";
foreach my $item (@{$content{$_}}) {
print "$item ";
}
print "\n";
}
或
#!/usr/bin/perl
use strict;
use warnings;
my %content;
open my $fh, '<file.txt' or die $!;
while (<$fh>) {
push @{$content{$1}}, $2 if /^(\S+)\s+(\S+)\s*$/;
}
close $fh;
print "$_\[email protected]{$content{$_}}\n" for keys %content;
或
#!/usr/bin/perl
use strict;
use warnings;
my %content;
open my $fh, '<file.txt' or die $!;
s/^(\S+)\s+(\S+)\s*$/{push @{$content{$1}}, $2}/e for <$fh>;
close $fh;
print "$_\[email protected]{$content{$_}}\n" for keys %content;
输出:
Bob April April March
Mary August
Robin December April
文件file.txt
与内容:
Bob April
Bob April
Bob March
Mary August
Robin December
Robin April
你并不需要初始化标量值为(空)的数组引用,它是自动完成的。 – TLP 2012-04-14 15:23:14
@TLP - 是真的,但对于一个新手来说,这有点不直观。 – DVK 2012-04-14 15:24:48