Home: Perl Programming Help: Beginner:
Search for a number beside a pattern & replace(increment) it



rahdirs
New User

Jan 29, 2017, 10:37 PM


Views: 1986
Search for a number beside a pattern & replace(increment) it

I have to read & modify certain data from .lib file. I could extract the line in which the data is present, but I could not extract the number associated with it.

Code
open my $fh, "<" ,".....lib" or die "$!"; 

while(my $line = <$fh>){
if( my ($xyz0) = $line =~m/xyz0 = (\S+)/){
print $xyz0,"\n"; # this prints 0.005 $
new_value = 0.9;
$xyz0 =~ s/$xyz0/$new_value/; # How do I enter this value to file ?}}

Code



      
    


Laurent_R
Veteran / Moderator

Jan 29, 2017, 11:12 PM


Views: 1981
Re: [rahdirs] Search for a number beside a pattern & replace(increment) it

Hi, I'm not completely sure I understand your problem, but if you want to modify your input file, this is usually not possible directly with ordinary sequential file. The usual way to do that it to write your output to another file and, then, when everything is OK and all files closed, to do the necessary house cleaning (i.e. rename input and output files in accordance with your needs).


BillKSmith
Veteran

Jan 30, 2017, 6:21 AM


Views: 1974
Re: [rahdirs] Search for a number beside a pattern & replace(increment) it

Perl runtime switches (Refer: perldoc perlrun) should be a big help. The switch /i automates all the file renaming details that Laurent described. Use of /p or /n could probably simplify your program to a "one-liner".
Good Luck,
Bill