Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Remember to process blank lines
I've got a Perl program that processes a data file 200 lines long.

15 of the lines are comments that start with a # (I test for those using ($line =~ /^#/), and 181 of the lines contain real data - in other words they start with a character that's not a # - my regular expression match reads ($line =~ /^[^#]/).

Whole file processed? No! There are 200 lines in the file but I've only processed 196. Although you'll think inventively that a line starts with either a hash or a character that's not a hash, there's a third case - a line that doesn't start with any character at all, i.e. a blank line. That's where my extra four lines have gone.

Rather than using two if statements to process all the lines, and match up the conditions, I should have used if and else. That way I can have my program securely find the "others" rather than having to work out potentially buggy code to do so.

Being Perl (where there's half a dozen ways to do anything), there are other good solutions such as writing the same condition twice but using the !~ operator in place of =~ to get the inverse set, or using an unless statement, or doing the test just once and saving the result as a boolean - $iscomment = ($line !~ /^\s*[^#]/);.
(written 2006-01-31 06:58:16)

 
Associated topics are indexed under
P212 - Perl - More on Character Strings

Back to
DWIM and AWWO
Previous and next
or
Horse's mouth home
Forward to
Loosing breath with Gerald

Some other Articles
Job vacancy - double agent wanted
Perl Regular Expressions - finding the position and length of the match
Looking for Python staff
Loosing breath with Gerald
Remember to process blank lines
DWIM and AWWO
Saving a MySQL query results to your local disc for Excel
What to do with milk
Short Linux and Perl courses for small groups
Woman screaming at me
1689 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho