Solved

Problem with perl

Posted on 2004-08-02
10
218 Views
Last Modified: 2010-03-05
I using Debian Linux 3.0 and Perl 5.6.1
I have this text file (the directory name : /home/student) that i want to insert the
keywords into the database.
 
 Yield Report
Job      : PQFP64    Start Date: 05/31/04
Tray     : Peak-14   Start Time: 14:40:35
Package  : PQFP-64   Sensor Ht : 239.17mils
Operator : CINDY     Run Time  : 16:11:53
Mach S/N : LSI318    Lot       : D4550027
________________________________________________
There's a table with 4 columns  in the text file.      

Perl Program
The below program enables me to extract keywords from multiple text file  
and insert the current directory of the files.

#!/usr/bin/perl

use DBI;
use File::Spec;
$directory = File::Spec->curdir();
print "$directorycld\n";

@ARGV = <*.txt>;
my ( $job, $tray, $date, $lot);
my( $dbh, $sth, $query );

$dbh = DBI->connect( 'dbi:mysql:rvsi');
$query = 'insert into test ( job, date, tray, lot, dir ) values ( ?, ?, ?, ? , ?)';
$sth = $dbh->prepare( $query );

while(<>) {
  if( /Job\s*:\s*(\S+)\s*Start Date\s*:\s*(\S+)/ ) {
    $job = $1;
    $date = $2;
    print "$job\n";
    print "$date\n";
  } elsif( /Tray\s*:\s*(\S+)/ ) {
    $tray = $1;
    print "$tray\n";
  } elsif( /Lot\s*:\s*(\S+)/ ) {
    $lot = $1;
    print "$lot\n";
  }
  if( ( $job ne '' ) && ( $date ne '' ) && ( $tray ne '' ) && ( $lot ne '' ) ) {
    # We have a full record

    $sth->execute($job, $date, $tray, $lot, $directory);

    $job = $date = $tray = $lot = '';

  }
}

$dbh->disconnect();

The output when the perl program is executed (database):

+----+--------+---------+----------+----------+------+
| no | job    | tray    | lot      | date     | dir  |
+----+--------+---------+----------+----------+------+
| 36 | PQFP65 | Peak-15 | D4550028 | 06/31/04 | .    |
| 37 | PQFP65 | Peak-15 | D4550028 | 07/25/04 | .    |
+----+--------+---------+----------+----------+------+

currently, i face this 2 problems:

1> the program only enable me to insert multiple text files with the filename ending
 with .txt but not all my filename have .txt (eg abc.txt and cde).
2> it is unable to insert the current directory path of the text files into the dir
table of the database (as shown in the output above)

how can i modify my perl program to enable this two function??
thanks
0
Comment
Question by:TPoly
  • 5
  • 3
  • 2
10 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 11700565
1> you can add additional strings to the globbing operator you're using. Something like
  @ARGV = <*.txt *.cde>;


2> to better see what the File::Spec::curdir method has given you, you should change the line reading

  print "$directorycld\n";

to read

  print "$directory\n";

=====

Now, as I read the doc, 'curdir' returns "." on UNIX and Linux. And this matches what you are seeing. To get the full pathname, you need to call rel2abs. So change the lines

use File::Spec;
$directory = File::Spec->curdir();

to be

use File::Spec::Functions qw(curdir rel2abs);
$directory = rel2abs( curdir());




0
 

Author Comment

by:TPoly
ID: 11701003
thanks it works..

however is it possible to include the name of the file in the directory?
And it still could not read files without .txt

Pls help
thank you.
0
 
LVL 20

Accepted Solution

by:
jmcg earned 400 total points
ID: 11701192
1> What are the specific files you wanted to read that it is not reading? What does your current glob expression look like?

2> OK, I've been misled by the names given to things. When you call something $directory, I think you mean you want the directory name. It sounds like what you really want is the file's absolute pathname.

It's a trick of the way that the <> operator works that the name of the current file is in the $ARGV variable.

Inside the 'if' statement before you do your $sth->execute, say:

my $fname = "$directory/$ARGV";

or

my $fname = rel2abs( $ARGV);

then use $fname instead of $directory in the parameter list. I'll leave it up to you to change the column name.

0
 

Author Comment

by:TPoly
ID: 11701535
thanks it works great for the directory path!!!
sorry, but this perl script is still unable to extract
data from those files without the .txt
0
 
LVL 20

Expert Comment

by:jmcg
ID: 11701686
1> What are the specific files you wanted to read that it is not reading? What does your current glob expression look like?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:TPoly
ID: 11701908
for example i have all this files (all the format of the files are the same):

test1, test2, test3.txt, test4.txt.

the above perl program only allows me to extract the data from test3.txt and test4.txt
i need to extract the data from all 4 files.

btw, what is current glob expression? how to find it?

thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 11701979
@ARGV = <test*>;
0
 

Author Comment

by:TPoly
ID: 11702039
oh ok i get what you mean

but my actual filenames will be like
DF758  KL133  AS122.txt DF098

so do i need to edit @ARGV = <test*>; to something else?

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 11702154
Are there files in the directory you don't want to process?
If not, you might use
  @ARGV = <*>;
otherwise, is there any way to distinguish the files you want from the files you don't want?
0
 

Author Comment

by:TPoly
ID: 11711552
thanks guys~ :)    
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now