Hi FishMonger, I was wondering if you could help me parse this line. Well actually I have hundreds of lines such as these.. but they're similar. I am working on a script that I'm writing from browsing and reading on the web, since I'm learning parsing lines. So far I got a lil. I was wondering if you could help me, since the web doesn't offer much to go on.

post some sample lines of data inside of the code tags to retain the formatting.

To me it looks like you could maybe use the split() function but without a better understanding of the data and what the columns represent its hard to say. -------------------------------------------------

Since your overall goal is unlcear I am not what else to suggest. Are you parsing the file line by line and doing something with the variables right then or are you building a large data set then doing something with the data? -------------------------------------------------

Since your overall goal is unlcear I am not what else to suggest. Are you parsing the file line by line and doing something with the variables right then or are you building a large data set then doing something with the data?

Really trying to follow with your piece of code.. but this part is all new to me.. guess there a whole lot left for me to learn. :)

0 thru 7 would be the index of the array value @words, so $words[1] is 90148 which is your $index scalar. Now you appear to want to split the date/time shown above into two seperate variables and reformat the date. That can be handled by a seperate function after you get the hang of using the module to parse the lines. -------------------------------------------------

Hey KevinR, I tried your script and sorta modified it somewhat to fit in my script. This script is working just like I wanted it and also with the date/time format. But I'd like for you to look at it and let me know if I wrote it right and if I used good Perl Practice on it, since I think it looks kinda confusing.

So let me know what you think please....

Code

#!/usr/bin/perl # This file is the parser.pl # This is a script that will parse data collected by collector.pl # and insert the data into a MySQL database.

use strict; use warnings; use Mysql; use Text::ParseWords; use Date::Parse;

OK then. Use the proper quotes. Use single-quotes for simple strings and reserve double-quotes for strings that have some type of interpolation of variables or meta sequences. For example, all of the below strings should be single-quoted:

Using the correct quotes in perl code is important to learn. Also good to know when not to use them, for example:

Code

$var = "$str";

there is no need to ever quote a scalar to assign it to another scalar yet we see people still doing that for some reason. Quotes are for creating new strings.

In your code you do stuff like this:

Code

# Index Variable my $index = $words[1]; print "Index:", $index, "\n";

You already have a scalar $words[1] that has the value you need, there is no need to create a new variable, $index. I understand you want to give the scalar a meaningful name, and that is a good practice to get into. But in this case I would not create the new variable just to use it in the INSERT statment, I would just add a line of documentation to make it clear that $words[1] is the index value from the file, and etc for the other variables. But that is a judgement call, because like I said, there are no major problems with your code. -------------------------------------------------

As of Msql-Mysql-modules 1.19_10 M(y)sqlPerl is no longer a separate module. Instead it is emulated using the DBI drivers. You are strongly encouraged to implement new code with DBI directly. See "COMPATIBILITY NOTES" below.

Hi Fish, That is absolutely right. I checked the file and I did find duplicated lines. My question is.. if there are duplicated lines, would it mess up the database eventually when trying to insert them into the database? I ask because I don't like to see error messages like those.

Duplicate lines in your file won't affect the db because the insert statement will simple fail and the original entry will remain intact.

You have a few options. 1) Leave it as is and ignore those warnings. 2) Disable warnings, which is not a good idea. 3) Use REPLACE instead of INSERT which does an update if it's a duplicate, otherwise it does an insert. 4) Preparse you file and remove the duplicates.