Question:
How can I get Perl to edit a database in a .txt file?
tim85a
2009-06-03 00:41:30 UTC
I'm trying to get Perl to make a database that looks like....

Mr Smith

1 Some Road
Some Town
County
Post Code

Mr A Nother

5 The Street
The Town
The County
Post Code

I have the names in $names and the address in $address, I can also get them printed in to data.txt how I want it to look, is it passable to get Perl to find and update the address using the name? The address might change length and I'm using Windows if that makes any difference at all.

Thank for your help.
Three answers:
Greg
2009-06-03 01:21:09 UTC
Bad Idea(TM)



Consider the name John Smith. Which John Smith do you mean?

-----------------

edit:



Pseudocode:



Open the file for reading.

Open a temp file for writing.

repeat until end of file or error:

* Read each record in turn

* If the record is not the one you wish to update write the record to the temp file

* If the record is the one you wish to update write the new record to the temp file



Close both files

Rename the original file (in case of error)

Rename the temp file to the original file name

If no error delete the original file otherwise rename the original file to its original name
Trevor
2009-06-03 02:46:25 UTC
You can use regular expressions to find and change portions of the text file. You would find the name and then match the section of text below it with something like "[0-9]+([\d\w ]+\s{2}){3}\d{5}". The \d{5} assumes a 5 digit numeric postal code, but could be changed to, for example, [\d\w ]{7} for Canadian postal codes.



http://www.regular-expressions.info/ is a good source for regex information.
martinthurn
2009-06-03 18:52:25 UTC
You could try the Tie::File module or the AnyData::Format::Paragraph module


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...