Monday 2 July 2007

Quick Perl template for Line and String Manipulation

We've got to tamper with some flat files (ultimately turning them into valid xml) - here's some quick notes (in the form of a *very* simple perl script) on the steps involved in:

- taking a filename as an argument (argv[0])
- opening the file
- keeping tabs on the line you're working on within the file (we use $. for this)
- getting access to the contents of each line within perl (we use $_ for this)

With this in mind, I can now use some extra arguments in the loop to start cutting up the data any which way. But here's a basic perl template for showing the line number and the string of text for each file in perl:

you would run this using something like:

./the-name-of-your-file.pl the-name-of-the-textfile.txt

No comments: