http://qs321.pair.com?node_id=11116842

kaushik9918 has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I am not an expert in Perl, hence requesting your help in this one. I have a user defined input which I need to parse and based on occurence of a pattern , need to replace certain lines on some text files based on the input file. So, for better understanding, I have got some text files , like a_copy.txt , a_paste.txt, a_cut.txt,b_copy.txt , b_paste.txt, b_cut.txt,c_copy.txt , c_paste.txt, c_cut.txt,d_copy.txt , d_paste.txt, d_cut.txt . Basically, my user input file looks like this:

NAME|VALUE = a TASK|VALUE = copy CAPS|VALUE = 0 PKG_TYPE|VALUE = premium NAME|VALUE = z TASK|VALUE = cut CAPS|VALUE = 0 PKG_TYPE|VALUE = premium NAME|VALUE = c TASK|VALUE = paste STACK|VALUE = 2 SHIP|VALUE = lowtier

Now , based on the input file, I first have to parse and if "NAME|VALUE = a" and the corresponding "TASK|VALUE = copy" , it should open a_copy.txt (if a_copy.txt doesn't exist, throw an error and exit, which is easy). Inside a_copy.txt, it has to check if the following lines with starting keywords like the input , i.e,

CAPS|VALUE = PKG_TYPE|VALUE =

exist and replace them in the a_copy.txt file with the input file values of "CAPS|VALUE = 0" and "PKG_TYPE|VALUE = premium". Any empty line in the input file should be ignored.

It should do this for all the text files corresponding to all the "NAME|VALUE = ??" and the corresponding "TASK|VALUE = ??" and replace all lines in the correct text file based on the input file values

.

I have managed to write a code which parses the input file for any format differences etc and report out, but I am stuck in writing the subroutine to do the replace the input file lines in the text files. My code is (although incomplete) is :

use Tk; use IO::Handle; #################### Check for correct usage of script################ +############ $mw = new MainWindow; my $num_args = $#ARGV +1 ; if ($num_args != 0) { $mw -> messageBox(-message=>"\nUsage: perl replay_file_changes.pl\n +"); exit; } ###################################################################### +############ ###################### User input #################################### +############################# print "\nPLEASE SPECIFY INPUT FILE\n"; $ip_file = <>; chomp $ip_file ; #check validity of user input if (-e $ip_file){ } else{ $mw -> messageBox(-message=> "\n$ip_file IS NOT FOUND\n"); exit; } ###################################################################### +############################ ###################### Real deal ##################################### +############################ open(INPUT_FILE, "<$ip_file") || die "\n!!!ERROR OPENING INPUT FILE. +EXITING SCRIPT!!!\n"; while(<INPUT_FILE>){ if ($_=~ /(.*) =\n/ ){ $mw -> messageBox(-message=> "\nFormat not + correct on line $. of input file. Exiting script\n"); exit; } elsif ($_=~ /(.*) =\s+\n/ ){ $mw -> messageBox(-message=> "\nFormat not + correct on line $. of input file. Exiting script\n"); exit; } elsif ($_=~ /(.*) = \s+(.*)/ ){ $mw -> messageBox(-message=> "\nFormat not + correct on line $. of input file. Exiting script\n"); exit; } elsif ($_=~ /^NAME\|VALUE = (.*)/ ){ &check(); } } sub check { #print "\n$cell_name\n"; my $name= $1; chomp $name; while(<INPUT_FILE>){ if($_=~ /^TASK\|VALUE = (.*)/){ $task_value= $1; } elsif($_=~ /^(.*) = (.*)/){ $line=$_; } elsif($_=~ /^NAME\|VALUE = (.*)/){ exit; } print "\n$name $task_value $line\n"; } }

And my output is:

a copy a copy CAPS|VALUE = 0 a copy PKG_TYPE|VALUE = premium a copy PKG_TYPE|VALUE = premium a copy NAME|VALUE = z a cut NAME|VALUE = z a cut CAPS|VALUE = 0 a cut PKG_TYPE|VALUE = premium a cut PKG_TYPE|VALUE = premium a cut NAME|VALUE = c a paste NAME|VALUE = c a paste STACK|VALUE = 2 a paste SHIP|VALUE = lowtier

Can someone please help me with subroutine to do the job here? PLease dont hesitate to ask if you got any further questions on this, Regards