We don't bite newbies here... much | |
PerlMonks |
Re^3: Match Line And Combine Into One Lineby kcott (Archbishop) |
on Jul 21, 2016 at 20:25 UTC ( [id://1168271]=note: print w/replies, xml ) | Need Help?? |
Firstly, this code won't compile as it contains a syntax error. Do not just post untested code! If you don't understand an error message, post the error you're getting and ask. Here's the offending line:
Take a look at "perlsyn: Statement Modifiers". The very first sentence starts with: Any simple statement may optionally be followed by a SINGLE modifier, ... "SINGLE" is emphasised for a very good reason: you can only use one statement modifier per statement. In the line I've identified, you've used two: if and for. Had you tried to run your code, you would have got a syntax error similar to the one in this example:
You have another issue that isn't an error but which would generate warning messages. The problem is that you haven't accounted for the file header line. You can skip this line with the simple expedient of adding this as the first line of your while loop:
$. is a special variable that holds the line count. Line 1 is the header line and next will effectively ignore it. See "perlvar: Variables related to filehandles" for a more detailed description. It's good that you've used the 3-argument form of open; it's less good that you've chosen global package variables to hold the filehandles and, indeed worse, that you've not chosen meaningful names. Once you get into the habit of using names like FILE, you'll use them often and, in all likelihood, multiple times in the same script or module: this is highly error-prone and can lead to bugs that are hard to track down. Instead, use lexical variables, with meaningful names, in the smallest possible scope; this greatly reduces the chances of errors and, in many cases, means you don't even need to use close as Perl will do this for you. It's also good that you're checking for I/O errors with "or die 'error message'" code; however, hand-crafting these messages is tedious and it's easy to leave out important information or forget to add them altogether. If you use the autodie pragma, Perl will perform this task for you: less work for you and less chances of errors. Putting all that together, along with your additional information, here's a new version of the script. Although not shown, my original script was pm_1168253_reformat_input.pl, this one's called pm_1168253_reformat_input_WITH_FILES.pl.
Note the anonymous blocks. The filehandles go out of scope once these blocks are exited: their reference counts are reduced to zero and Perl performs an implicit close. Here's the input file:
And here's the output file before and after running the script:
As before, you may need a different ordering for your output but I'm still in the dark as to what you require. — Ken
In Section
Seekers of Perl Wisdom
|
|