Free to a good home. No warranties expressed or implied. If I have an attribution in the snippet, please keep it. Otherwise, you're on your own.
Parsing comma delineated output:
This was written on the fly in response to an individual who needed to parse comma delineated output that sometimes had commas in the field itself, like so:
4/11/99,18:30,20:00,Women's Discipleship Group,A,Normal,,
4/11/99,19:00,21:00,"Jr. High Meeting, Sr. High Home Group",,Normal,,
# ----------------------------- # start snippet, formatted for readability # open the file, start chunking through it, line by line while (defined ($line = FILE)) { next if $line =~ /^\s/; # Skip blank lines if ($line =~ m/^(\d{1,2}\/\d{1,2}\/\d{2,4}), # capturedate (\d{1,2}:\d{1,2}), # capture tstart (\d{1,2}:\d{1,2}), # capture tend (.+), # capture verbiage ([A-Z]*), # capture code (\w+), # capture type (.+), # capture misc $/x) { # end regex with "/x" # start consequences # set variable to regex captures ($date, $tstart, $tend, $verbiage, $code, $type, $misc) = ($1, $2, $3, $4, $5, $6, $7); # do the html or whatever... print "$date, $tstart to $tend
$verbiage<\td>\n"; ) else { # find glitches print "Bad Record: $line\n"; }; # end if # do other stuff, like finish off table here }; # end while # --------------------------------The key here is the parentheses in the regex, which can be used to collect the items you want in special variables $1 to $n (where n is a number). Please note that this code has not been tested on a "live" system (to my knowledge) and may contain errors. Pobody's nerfect.
~~ Got any goodies that you think I should include? Drop me an e-mail at ljl @ laubenheimer.net ~~
Page created: March 28, 2000. Page last changed/tweaked on 08/20/2002