goosefairy has asked for the wisdom of the Perl Monks concerning the following question:
I have a series of ascii files in a directory with the following format:
<SENDER> Sender NameThe Message part will go on for several lines and could have punctuation, spaces and special characters which would need to be preserved. On our pages the message always gets displayed with <pre> tags.
<MESSAGE> is always the last field and the actual message always starts on the next line.
My ultimate goal is to put this information into a mySQL database table where the column names correspond to the <TEXT> part and the data is what comes after that.I can get the information in all but the <MESSAGE> field. Following is my code:
#!/usr/bin/perl -w $dirname = "2003/"; #----------------------------------------------- sub field_found(@_) { my $line = shift; my $fld = shift; my $val = shift; my $pos = index($line,$fld); if($pos == 0){ # found field my $flen = length $fld; my $llen = length $line; $$val = substr($line,$flen,$llen); } # found field } # opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; while (defined($file = readdir(DIR))) { open(INPUT, $dirname . $file) or die; while($line=<INPUT>) { chomp($line); field_found($line,"<SENDER>",\$sender); field_found($line,"<TO>",\$to); field_found($line,"<FROM>",\$from); field_found($line,"<MESSAGE>",\$message); @array = ("$sender","$to","$from","$message"); } close(INPUT); open INPUT, ">2003/clean/$file.clean" or die; # this here just to check array contents print INPUT "Sender: $array[0]\n"; print INPUT "To: $array[1]\n"; print INPUT "From: $array[2]\n"; print INPUT "Message: $array[3]\n"; close(INPUT); } closedir(DIR);
How in the world do I go about this? Am I totally off-track?
Thanks for any help or just pointing in the right direction.
goosefairy
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: is array the best way to handle this?
by Ovid (Cardinal) on Sep 08, 2003 at 19:10 UTC | |
by goosefairy (Novice) on Sep 08, 2003 at 19:52 UTC | |
by dragonchild (Archbishop) on Sep 08, 2003 at 20:02 UTC | |
by goosefairy (Novice) on Sep 08, 2003 at 21:07 UTC | |
|
Re: is array the best way to handle this?
by chromatic (Archbishop) on Sep 08, 2003 at 19:09 UTC |