2015-10-20 39 views
-1

我比較兩個字符串,如(textid, citeid)如何聲明字符串比較無限循環?

如果兩個ID (textid, citeid)相等對應uniqueid應在textid在整個文本引用部分取代。

MWE:

#!C:\Strawberry\perl\bin\perl 

use strict; 
use warnings; 
use 5.010; 
open(IN,"$ARGV[0]\.html")||die("Input LaTeX filename without .html extension\n"); 
local $/; 
my $String_Match=<IN>; 

my ($textid) = $String_Match =~ m/textid="(.*?)"/; 
my ($citeid) = $String_Match =~ m/citeid="(.*?)"/; 
my ($uniqueid) = $String_Match =~ m/ref uniqueid="(.*?)"/; 

if ($textid eq $citeid) 
{ 
     if ($String_Match=~m/textid="(.*?)"/s) 
       { 
       $String_Match =~ s/textid="(.*?)"/textid="$uniqueid"/si; 
       } 
} 

print $String_Match; 

字符串比較eq功能不工作等(同時或做-同時)

上面MWE執行條件僅一次無限循環。但是我的所需輸出textid在整個文件中應該被替換爲uniqueid

我的輸入文件:

Text Citation Part 
    <xref textid="Narain:1982">1</xref> 
    <xref textid="Nasir:2012">2</xref> 
    <xref textid="Yao:2011">3</xref> 
    <xref textid="Cogley:1968">4</xref> 

    Reference Citation Part 
    <ref uniqueid="j_zna-2014-0260_ref_001"><citeid="Narain:1982"></ref> 
    <ref uniqueid="j_zna-2014-0260_ref_002"><citeid="Nasir:2012"></ref> 
    <ref uniqueid="j_zna-2014-0260_ref_003"><citeid="Yao:2011"></ref> 
    <ref uniqueid="j_zna-2014-0260_ref_004"><citeid="Cogley:1968"></ref> 

我所需的輸出文件:> out.txt

Text Citation Part 
<xref textid="j_zna-2014-0260_ref_001">1</xref> 
<xref textid="j_zna-2014-0260_ref_002">2</xref> 
<xref textid="j_zna-2014-0260_ref_003">3</xref> 
<xref textid="j_zna-2014-0260_ref_004">4</xref> 

Reference Citation Part 
<ref uniqueid="j_zna-2014-0260_ref_001"><citeid="Narain:1982"></ref> 
<ref uniqueid="j_zna-2014-0260_ref_002"><citeid="Nasir:2012"></ref> 
<ref uniqueid="j_zna-2014-0260_ref_003"><citeid="Yao:2011"></ref> 
<ref uniqueid="j_zna-2014-0260_ref_004"><citeid="Cogley:1968"></ref> 

請指點如何執行邏輯.....

+0

故意輸入和輸出的空白區別? – simbabque

+0

@simbabque我無法理解。你在說什麼? – Vetri

+0

我會逐行瀏覽文件,以textid作爲關鍵字將數字存儲在散列中,然後將所有唯一ID存儲在那裏,然後從中構建您的輸出。你當然也可以像你這樣做,然後抓住所有textids到數組等,但我會逐行找到更簡單。 – bytepusher

回答

0

字符串比較eq功能不適用於無限循環(while或do-while)

它不會。您實際上並未更改循環中$textid$citeid的值,因此您的條件爲總是爲真。

我假設你有一個類似的問題,您剛纔的問題:How to match the two same id and import the required ids

但問題依舊。 A while循環繼續執行,直到其測試條件不再滿足。

因爲你不改變你的循環中$textid$citeid,你只是將相同的文本轉換遍地$String_Match(也許。如果它永遠不會在第一時間匹配,甚至沒有應用的變換,你只是去轉轉什麼都不做。

事實上,這是相當難以在第一時間說你打算你的while(或if)環路什麼擺在首位實際上做

知道什麼可能是有用的是,perl有hash es。您可以在perldata中閱讀它們 - 它們讓您可以將鍵和值關聯起來。

等,例如:

my $input_text = '<ref uniqueid="j_zna-2014-0260_ref_001"><citeid="Narain:1982"></ref>'; 

my ($cite_id) = $input_text =~ m/citeid="(.*?)"/; 
my ($unique_id) = $input_text =~ m/uniqueid="(.*?)"/; 

my %unique_id_for; 
$unique_id_for{$cite_id} = $unique_id; 

這將構建你一個哈希關聯「引用標識」,並從您的參考列表「唯一ID」。

'Narain:1982' => 'j_zna-2014-0260_ref_001' 

然後,您可以在腳本的其餘部分將其用作搜索和替換的一部分。