作者salagadoola (南瓜)
看板perl
标题Re: [问题] 重复性的资料
时间Fri Aug 17 13:53:08 2007
※ 引述《roy8130 (reputation)》之铭言:
: 当重复遇到X=24 & Y=1时候 把更新的 value , 覆盖到第一个
: X=24 & Y=1的位置
: 每ㄧ个集合为 {
: X=24
: Y=1
: 14
: 2
: 6
: 30
: }
: test.txt ------------------------> output results
open FH, "<", "test.txt";
my $str = join("", <FH>);
close FH;
my %hash; ## 存放资料,以 X=...\nY=...\n 为 key
my @arr; ## 纪录每一个 key 出现的前後顺序
while ( $str =~ s/(X=\d+\nY=\d+\n)([^X]+)// ) {
if ( not exists $hash{$1} ) {
push @arr, $1; ## 遇到没看过的 key 就放进 @arr 里
}
$hash{$1} = $2;
}
foreach my $str (@arr) {
print $str, $hash{$str};
}
注:这里用 X 来判断资料间的分隔,所以如果 value 里有 X 的话程式就会错。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.23.102