看板 Perl 關於我們 聯絡資訊
※ 引述《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