CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



rahdirs
New User

Jan 29, 2017, 10:37 PM

Post #1 of 3 (1262 views)
Search for a number beside a pattern & replace(increment) it Can't Post

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

Post #2 of 3 (1257 views)
Re: [rahdirs] Search for a number beside a pattern & replace(increment) it [In reply to] Can't Post

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

Post #3 of 3 (1250 views)
Re: [rahdirs] Search for a number beside a pattern & replace(increment) it [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives