Hi Kit,

I am not sure whether you deliberately choose to send the email not to the
list, but it might have been a wise thing considering the long list of
emails. ;-) (which I deleted right away for your privacy).

Unfortunately I am no PHP expert but rather familiar with Perl but I don't
think that is a major problem since you know the basict and shuld be able to
translate the code with the help on some good online help and your own
experience. Otherwise repost to the mysql list (or php list).

Text sample
Last    First    ID    Email
Lastname    Firstname M.    111111111    [EMAIL PROTECTED]
Lastname    Firstname M.    111111112    [EMAIL PROTECTED]
Lastname    Firstname M.    111111113    [EMAIL PROTECTED]

In Perl the code to grab the information out of the textfile would look like
Sorry for possible code errors - was the first thing in the morning:

    open INPUT, "/path/data.txt" || die "Cannot open input file!";
    my $counter = 0;
    my $last;
    my $first;
    my $id;
    my $email;

    while (my $line = <INPUT>) {
        counter++;
        next unless counter>1;
        $line =~ /^(\w+)\s+(.+)\s+(\d+/)\s+(\S+)\s*$/;
         
# grabs the first group of word characters till the first white space
character
        $last = $1;
# grabs the second all the characters until the white space before the
digits of the ID (since you might have  a middle initial with a dot)
        $first = $2;
# grabs all the digits between white space characters
        $id = $3;
# grabs all non white space characters before the end of the line
        $email = $4;
# the last \s* is for any number of trailing white space characters at the
end of the line which you probably don't need but it doesn't hurt

        your insert statement to put the data into MySQL
    }
    close INPUT || die "Cannot close input file!";

Some explanation to help you with the translation.
1) I declare the variables ('my' statements), you might not have to do that.
   Variables are preceded by a '$', I think you don't need that in PHP.
2) I inserted the counter variable and the next statement to skip the first
   line with field headers.
3) 'while' loops through all lines in the input file and assigns them to
   $line one by by one.
4) The matching part of the line within parenthesis () gets put into the
   anonymous variables $1 - $4 form left to the right
5) Square brackets [] indicate a character class
6) The backslash \ is the escape character (needed for special expressions).
7) \s = white space character; \S non-white space character
8) \d = digits = [0-9]; \w word character
9) * = 0 or any number of the preceding expression
10) + = 1 or any number of the preceding expression
11) ^ = start of the line: $ end of the line
12) . = any character except a new line (\n)
13) # = comment

I hope that gets you started.

Hannes

And remember clicking just the reply button in response to messages from the
MySQL list will send just to the person who posted the message, you have to
add the mysql-list address yourself.




 
On 7/29/01 1:05 AM, "Kit Kerbel" <[EMAIL PROTECTED]> wrote:

> Sorry about the vagueness.  The language I am using is PHP4.  I am needing a
> php script that will parse the text file into the mysql database.  I have
> attached a sample of the datafile.
> Thanks for your help,
> Kit
> 
> 
> ----Original Message Follows----
> From: Hannes Niedner <[EMAIL PROTECTED]>
> To: Kit Kerbel <[EMAIL PROTECTED]>, <[EMAIL PROTECTED]>
> Subject: Re: Parsing text file into mysql database.
> Date: Sat, 28 Jul 2001 14:00:03 -0700
> 
> You need to provide some more details about your task. What programming
> language you want to use, and probably a sample from the file that matches a
> repetitive subunit within the text. This information is needed since there
> is a high chance that you need to exploit regular expression matching for
> your goal.
> 
> Hannes
> 
> 
> On 7/28/01 10:46 AM, "Kit Kerbel" <[EMAIL PROTECTED]> wrote:
> 
>> Hello,
>> I was wondering if anyone could give any advice on how to go about
> parsing a
>> plain text file into a mysql database.  I have a class Roster table that
> I
>> need to parse a text file into.  I already have the uploading part of the
>> file done.  Now I just need to add some code to parse the file into the
>> database.  Any suggestions would be appreciated.
>> 
>> Thanks,
>> Kit
>> 
>> _________________________________________________________________
>> Get your FREE download of MSN Explorer at
> http://explorer.msn.com/intl.asp
>> 
>> 
>> ---------------------------------------------------------------------
>> Before posting, please check:
>>  http://www.mysql.com/manual.php   (the manual)
>>  http://lists.mysql.com/           (the list archive)
>> 
>> To request this thread, e-mail <[EMAIL PROTECTED]>
>> To unsubscribe, e-mail
>> <[EMAIL PROTECTED]>
>> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
>> 
> 
> 
> ---------------------------------------------------------------------
> Before posting, please check:
>   http://www.mysql.com/manual.php   (the manual)
>   http://lists.mysql.com/           (the list archive)
> 
> To request this thread, e-mail <[EMAIL PROTECTED]>
> To unsubscribe, e-mail
> <[EMAIL PROTECTED]>
> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
> 
> 
> 
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
> 


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to