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:
how to replace parameters in a file

 



snowpl10
New User

Jul 22, 2016, 10:23 AM

Post #1 of 3 (1516 views)
how to replace parameters in a file Can't Post

Hi all,

I have a file df.txt in the following. In each line of df.txt, there are four place holders x1 ~ x4. Then I have another file param.txt, while each row has four values. I want to write a script, while for each loop, the first row in the param.txt file will replace x1~ x4 in all rows in the df.txt file. So if there are n rows in the param.txt file, there will be n df.txt files. After each loop, the filled df.txt file will be in a specific directory. How to realize this through a script? Thanks.

df.txt
1 5.5 6.2 3.3 5.6 x1 x2 x3 x4 6.7 8.0
2 6.7 6.1 3.5 5.5 x1 x2 x3 x4 6.6 8.9
3 6.4 6.2 3.4 5.5 x1 x2 x3 x4 6.7 9.1
4 6.0 5.5 3.6 5.7 x1 x2 x3 x4 6.4 9.0

param.txt
2.3 2.6 7.8 9.1
2.1 2.5 7.5 9.8
3.4 3.3 7.6 8.8


FishMonger
Veteran / Moderator

Jul 22, 2016, 1:55 PM

Post #2 of 3 (1498 views)
Re: [snowpl10] how to replace parameters in a file [In reply to] Can't Post

Are the fields to be replaced always in the same positions as shown in your sample? If so, the an easy approach would be to loop over both files putting the fields into arrays and use splice to do the replacement.


Laurent_R
Veteran / Moderator

Jul 22, 2016, 2:26 PM

Post #3 of 3 (1495 views)
Re: [snowpl10] how to replace parameters in a file [In reply to] Can't Post

It does not seem too complicated, but maybe it would be useful for better comprehension to get an example of the expected output with the input you provided.

 
 


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

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