如何声明字符串比较无限循环?

时间:2015-10-20 14:49:47

标签: perl

我比较像(textid, citeid)这样的两个字符串。

如果两个ID (textid, citeid)相等,则应在{strong>文本引文部分中的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函数不适用于无限循环(while或do-while)

上述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>

我需要的输出文件:&gt; 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>

请建议如何执行逻辑......

1 个答案:

答案 0 :(得分:0)

  

字符串比较eq函数不适用于无限循环,如(while或do-while)

不会。您实际上并未在循环中更改UILabel$textid的值,因此您的条件始终为true。

我认为您在上一个问题中遇到了类似的问题:How to match the two same id and import the required ids

但问题仍然存在。 $citeid循环继续执行,直到不再满足其测试条件为止。

由于您不会在循环中更改while$textid,因此您只需将相同的文字转换反复应用于$citeid(也许。它从来没有匹配过,变换甚至没有应用,你只是在圈内无所事事。

事实上,首先要说明你的$String_Match(或while)循环到实际上是什么首先。

可能有用的是perl有if es。您可以在perldata中了解它们 - 它们可以让您关联键和值。

喜欢,例如:

hash

这将构建一个哈希关联&#39;引用ID&#39;和&#39;独特的ids&#39;来自你的参考清单。

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; 

然后,您可以在脚本的其余部分中使用它作为搜索和替换的一部分。