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.
goosefairyIn reply to is array the best way to handle this? by goosefairy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |