Ingenious Perl Monks,
I'm trying to search for the word "END" in a file (which occurs only once), then replace it with several lines of text. I am having trouble in doing this correctly. I tried reading and writing from the same file, but get incorrect results. I am slurping all of the data to be inserted instead of "END" into one scalar, then trying to add that in place. The code that I am currently using is here:
$COMPFILE = IO::File->new;
#!!!!!!!!!!!!!!!!!!!!!!!!! SCALAR SLURP !!!!!!!!!!!!!!!!!!!!!!!!
$comptmplt_file="comp_template.txt";
{
local $/;
open SLURP, $comptmplt_file or die "sudden death";
$comptmplt=<SLURP>;
close SLURP or die "cannot close $comptmplt: $!";
}
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$COMPFILE->open("+< comp_no_rods.txt" or die "Can't open");
print "$comptmplt";
until ($COMPFILE -> eof){
$compline = $COMPFILE->getline();
if ($compline=~ /END/){;
$compline=~ s/END/$comptmplt/;
print $COMPFILE "$compline";
}
}
I think some error is occuring, because the text in the scalar replace variable has the word "END" in it as well, but the code doesn't get stuck in an infinite loop, it just adds some screwy lines along with the correct ones. I am sure I am doing this backwards and someone here has a better idea and I'm all for hearing it. Any further questions are welcomed as well. Thanks!
Update:
I figured it'd be easier if I posted what was in each file:
The end of the file to be altered (note the "END" is what I want to change)
! ==========================================================
TITLE T2 C1 2V-03-12100 9193 - 9406 EFPH. UTBR STEG NR 018
OPTION Burnup,Neu3,NoSearch,TTable,FlwTot,NoPowEnd
PRINT KEYPAR
INIT /sompath/dist-9193.dat=HIST
SAVE /somepath/dist-9406.dat=POWER,HIST,TM,TIPNEU,PRMNEU,CRWDR,
SAVE * PINPOW,PINBUR,PINLHR,PINFIM,PINDRY
POWER 88.900
FLOW 7737.0
PRESS 70.000
CONROD ALL=100
BURNUP 213.0000, 1, EFPH, Straight-Euler
END
The text to be inserted in place of the "END"
! ==========================================================
TITLE T2 C1 2V-03-12100 BEGIN - FINISH EFPH. UTBR STEG NR 018
OPTION Burnup,Neu3,NoSearch,TTable,FlwTot,NoPowEnd
PRINT KEYPAR
INIT /somepath/dist-BEGIN.dat=HIST
SAVE /somepath/dist-FINISH.dat=POWER,HIST,TM,TIPNEU,PRMNEU,CRWDR,
SAVE * PINPOW,PINBUR,PINLHR,PINFIM,PINDRY
POWER 100.0
FLOW 6500
PRESS 70.000
CONROD ALL=100
BURNUP 500.0000, 1, EFPH, Straight-Euler
END
I will do this once, do some calculations, check to see if I have satisfied a criteria. If I haven't, I'll add more data to the file by changing the "END" in the exact same way above, with the file growing by another addition of data, then re-run a calculation. (Hopefully that makes sense). If you want to see the output I currently get, feel free to ask. Thanks again.