[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Problem with perl

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
TPoly
Asked:
TPoly
  • 5
  • 3
  • 2
2 Solutions
 
jmcgOwnerCommented:
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
 
TPolyAuthor Commented:
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
 
jmcgOwnerCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TPolyAuthor Commented:
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
 
jmcgOwnerCommented:
1> What are the specific files you wanted to read that it is not reading? What does your current glob expression look like?
0
 
TPolyAuthor Commented:
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
 
ozoCommented:
@ARGV = <test*>;
0
 
TPolyAuthor Commented:
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
 
ozoCommented:
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
 
TPolyAuthor Commented:
thanks guys~ :)    
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now