私は弱い perl ユーザー (および配列の操作者) であり、この問題は私を困惑させています。誰かが助けてくれることを願っています!
次のタイプのデータを含むソース ファイルがあります (大幅に簡略化されています)。
URL: 22489196
Keywords: Ball, Harga, Call, Dall, Eall, Jarga, Fall
URL: 22493265
Keywords: Hall, Iall, Yarga, Jall, Zarga, Kall
アルファ順序を中断する単語 (Harga など) は「修飾子」です。私が必要とする最終結果は次のとおりです。
22489196
Ball--Harga
Call
Dall
Eall--Jarga
Fall
22493265
Hall
Iall--Yarga
Jall--Zarga
Kall
さまざまな「for」ループを試して、項を 2 番目の配列にプッシュし、項の条件付き連結で元の配列をシフトしましたが、それでも項が欠落しているか余分な項が残っています。誰でもこれを行う方法を提案できますか? よろしくお願いします!
追加: これは私の乱雑なコードの一部の繰り返しです:
while (<FILE>) {
if (/URL\:/) {
print "$_\n";
}
if (/Keywords\: /) {
s/Keywords\: //;
chomp();
my @terms = split ', ', $_;
my @bakterms = reverse @terms;
my $noTerms = @terms;
my $IzItOdd = $noTerms%2;
#my $ctr = $noTerms++;
for ($i = 0; $i <= $#bakterms; $i++){
my $j = $i+1;
if ($j <= $#bakterms) {
my $one = $bakterms[$i];
my $two = $bakterms[$j];
if ($two gt $one) { # i.e., if $two is alphabetically AFTER $one
push @ary3, $bakterms[$i];
$disarry = 1;
my $interloper = $bakterms[$j+1].= "--" . $two;
push @ary3, $interloper;
shift @bakterms;
#$ctr--;
shift(@bakterms);
#$ctr--;
}
else {
push @ary3, $bakterms[$i];
#shift(@bakterms);
shift @bakterms;
$disarry = 0;
}
}
}
@ary3 = sort @ary3;
foreach my $term (@ary3) {
print "** $term\n";
}
@ary3 = ();
print"\n";
}
}
exit 0;