Question : I need to get to the Nth field in the Nth line of a text datafile, change it and save it back to the text file.

Hi all, for most of you this must look easy, but i have hard time with it, here it comes:

##########My text file lets call it (textfile.txt) #########

~~~id_42~~~field1~~~field2~~~field3~~~field4~~~...field_nth~~~
~~~id_43~~~field_a1~~~field_a2~~~field_a3~~~field_a4~~~...field_nth~~~
~~~id_44~~~field_b1~~~field_b2~~~field_b3~~~field_b4~~~...field_nth~~~
############# End of file #####################

The id_number is unique for each line, asigned by the program the time that created the line.

By using the id_number I need to get to that line and make changes to the fields,
then save the changed line back to textfile.txt with the same id_number.

I have the (id) like:
$id = $form{'id'};

There will be only 1 admin at the time, so no worries about 2 instances writing at the same time,
and no, we don't  know which line in the  textfile.txt contain the id# that we need to make changes on, so everything go by the id number, and all data is saved at the bottom of the textfile.txt.

Having this in mind can you please give me an example code on how to do this?

Thank you in advance for looking at this.






Answer : I need to get to the Nth field in the Nth line of a text datafile, change it and save it back to the text file.

You mean like this?
{local @ARGV=("textfile.txt"); local $^I='';
    while( <> ){  
        if( /\b\Q$id\E\b/ ){
            $_ = join$delimiter','',$id,$field1,$field2,$field3,$field4 ."\n";
        }
        print;
    }
}
Random Solutions  
 
programming4us programming4us