Thanks very much -it's working! But don't ask me why, could you do me a favour and comment code for a poor (part time) programmer like me. Thanks Martin | [reply] |
Sure :)
This simplifies the explanation of each of the lines to just that relevant to this program. Look the constructs up in the docs for the full ins & outs of each.
- c:\test>thisScript.pl yourFile > theNewFile
This command says run thisScript.pl, have it read yourFile and redirect its output to theNewFile.
- while( <> ) {
This says: read lines from the file supplied on the command line (<>), one at a time and put each line into the somewhat magical variable $_.
Repeat this process while there are more lines to read from that file, and then end the loop.
See the discussion under the heading "IO operators" in perlop. Particularly the paragraph starting: "The null filehandle <> is special:". (Be warned: That link goes to a long page and the apparently clickable link "IO-operators" doesn't seem to work. You'll have to scroll down manually (or search in page) and it is a long way down. Let the page load fully first!)
- my @fields = unpack 'A31 A31 A26 A13 A9 A11 A31 A26 A26 A1', $_;
This takes each line (in $_) and gives it to unpack to deal with.
The first argument (called the 'template'), tells unpack how you want the line broken up.
A31 says the first field is 31 characters long. The 'A' part also says that if there are any trailing spaces in that field, you are not interested in them so throw them away before returning the field to you.
It does this for all the fields in each line and puts the results into @fields. $fields[ 0 ] will be the first field in the line, $fields[ 1 ] will be the second, and so on.
unpack is documented in perlfunc. (However, much of the template documentation is under pack and you have to mentally reverse the descriptions.)
- say join "\t", @fields;
This says: takes all the fields (which now have any trailing spaces stripped), and join them back together into a single string putting a tab character ("\t") between each pair. join returns the resultant single string.
See join.
say is a new 5.10 built-in that essentially does the equivalent of print <args...>, "\n";
It just prints the newly formed tab separated line to STDOUT., where the redirection > theNewFile puts into the file.
- }
Ends the loop when there are no more lines. And that ends the program, which closes the input file that was oopened automatically for you by the 'diamond operator' (<>).
If any of that is unclear or needs expanding, please ask. When stuff is very familiar, it's quite hard to remember what you found unintuative when you first encountered it.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |