I have a series of ascii files in a directory with the following format:

<SENDER> Sender Name
<TO> To list
<FROM> User Name
<MESSAGE>
Text text text text text...

The 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

In reply to is array the best way to handle this? by goosefairy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.