这里我有一个abc.txt文件:
HWND Handle = this->m_hWnd
然后我要打印出来:
aaa,1000,kevin
bbb,2000,john
ccc,3000,jane
ddd,4000,kevin
我的Perl脚本是:
kevin
john
jane
我的想法是将值存储到哈希中并为每个值创建键。我怎么能在###行?
答案 0 :(得分:1)
您已声明@store
,然后使用%store
。我不明白为什么你这样做,但下面的代码将给你欲望输出。首先读取输入文件,拆分数据然后删除重复项。
use strict;
use warnings;
my $infile = $ARGV[0];
open my $fh, "<", $infile or die "An input file is required as argument: $!";
my %store;
while(my $line = <$fh>)
{
chomp($line);
my @data = split /,/, $line;
my @removeduplicate = (grep { !$store{$_}++ } @data)[2];
foreach(@removeduplicate){
if( $_ ne ''){
print "$_\n";
}
}
}
close $fh;
输出:
kevin
john
jane
答案 1 :(得分:0)
嗯。这取决于你想要什么。也许这个例子可以帮助你:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper; #for debug if you want
my $infile='abc.txt'; #or ARGV[0] whatever it is
my $fh;
open $fh,'<',$infile or die "problem with $infile $@ $!";
my $inputline;
my %Storage;
my @Values;
while (defined($inputline=<$fh>)) {
chomp $inputline;
@Values=split ',',$inputline;
if (@Values != 3) {
warn "$inputline has formatted badly";
next;
}
#warn if exists $Storage{$Values[1]}; #optional warning for detected duplicates
$Storage{$Values[1]}=@Values[0,2]; #create hash data
#duplicates will be removed automaticly
}
close $fh;
print Dumper \%Storage; #print how perl it stores
foreach my $Key (keys %Storage) { #example loop
print @{Storage->{$Key}},"\n"; #do anything
}
我希望这个模板对你来说已经足够了。