문제

I do not have more idea why in below code, replacement (indexRecord) doesn't work?

indexRecord = 0
bankFileRecord = 3
#add records
data = File.read("./Model/Bank/Record.xml")
while indexRecord < bankFileRecord do
    if indexRecord == 0
        replacedRecord = data.gsub(/(Valore=")(.*")/i, "\\1#{indexRecord+1}")
        f.puts replacedRecord
        indexRecord += 1
    else 
        replacedRecord = replacedRecord.gsub(/(Valore=")(.*")/i, "\\1#{indexRecord+1}")
        f.puts replacedRecord
        indexRecord += 1
    end
end

In all replaced elements I get Valore=0 (or other index declarate number). But when I printing indexRecord for debug, value is incrementing in print function.

I would like to have in each replaced line, value (Valore) incremented by 1.

Currentl in output i Have

<Date_records>
    <Date_general>
        <first TAG="A" Valore="1/>
        <second TAG="B" Valore="1/>
        <third TAG="C" Valore="1/>
    </Date_general>
</Date_records>
<Date_records>
    <Date_general>
        <first TAG="A" Valore="1/>
        <second TAG="B" Valore="1/>
        <third TAG="C" Valore="1/>
    </Date_general>
</Date_records>
<Date_records>
    <Date_general>
        <first TAG="A" Valore="1/>
        <second TAG="B" Valore="1/>
        <third TAG="C" Valore="1/>
    </Date_general>
</Date_records>

Input looks like this

<Date_records>
    <Date_general>
        <first TAG="A" Valore="X"/>
        <second TAG="B" Valore="X"/>
        <third TAG="C" Valore="X"/>
    </Date_general>
</Date_records>
도움이 되었습니까?

해결책

I've tested the code you posted and it seems to work fine, so there must be something wrong with the parts you aren't showing us. If I had to guess I'd say that maybe bankFileRecord is 0 so the body of the while loop is never executed?

Edit: I made a mistake when I ran your code myself, there was a small detail I didn't see. The problem is with your replacement string:

replacedRecord = data.gsub(/(Valore=")(.*")/i, "\\1#{indexRecord+1}")

should be changed to:

replacedRecord = data.gsub(/(Valore=")(.*")/i, "\\1#{indexRecord+1}\"")

you missed the closing speech mark. (you need to change it in both the gsubs)

다른 팁

Missing '\"' make situation that in the ELSE Regular expresion does not much. Big thanks @Doydle again and great sorry for messy issue :/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top