I'm trying to create a script that would read the content of a text file and use that content to populate a database. Here is what i got so far...
#!/perl/bin/perl use strict; use File::Copy; # Used to buffer files-to-send use DBI; use Switch; #Main# #Scan File# my $scan_dir = "C:\\Log"; my $error_file = "error_log"; my $filename; my $data; my @name; my @info; opendir DIR, $scan_dir; open( ERROR, ">>$error_file" ); # Search cache directory for csv files while ( $filename = readdir(DIR) ) { if ( $filename =~ /\.txt$/i ) { $data = Read_File( $scan_dir, $filename ); } } close(DIR); close(ERROR); #Read File# sub Read_File { my ( $scan_dir, $file_name ) = @_; print "Read file $scan_dir\\$file_name\n"; open (CVS, ">>Test.txt") or die "Cannot write CVS\n "; open( FILEREAD, "$scan_dir\\$file_name" ) or die "cannot open $fil +e_name\n"; my $Test = <FILEREAD>; my @Content = split( / /, $Test ); my ($EquipmentID, $Chamber, @LotID, $Recipe, $AlarmType, $Sensor, +$Signal_Level, $Alarm_Upper_Limit, $Alarm_Lower_Limit); my ($UserCode, $Code, $Date_Submited, $Time_Submited); my $number; my (@array, @Comment); my $count; my $regex; for ( $number = 0 ; $number <= $#Content ; $number++ ) { @array= split(/=/,$Content[$number] ); switch ($array[0]) { case (/EQPID/i) { print "EQPID = $array[1]\n"; $Chamber = substr ($array[1], -1, 1); switch ($Chamber){ case (/A/i) {print "Chamber 1\n";} case (/B/i) {print "Chamber 2\n";} case (/C/i) {print "Chamber 3\n";} case (/D/i) {print "Chamber 4\n";} } } case (/AlarmType/i) {print "AlarmType=$array[1]\n";} case (/LotID/i) { print "$array[1]\n"; @LotID = split( /-/, $array[1] ); print "LotID = $LotID[0]\n"; print "Wafer_Flow = $LotID[1]-$LotID[2]\n"; } case (/Comment/i) { @Comment = split( /,/, $array[1] ); for ($count = 0; $count<=$#Comment; $count++) { if ($count==1) { print "Sensor = $Comment[$count]\n"; + } elsif ($count==2) { print "Signal_level = $Comment[$count]\n"; } elsif ($count==3) { print "AUL = $Comment[$count]\n"; } elsif ($count==4) { print "ALL = $Comment[$count]\n"; } elsif ($count==5) { print "Recipe = $Comment[$count]\n"; } elsif ($count==6) { print "Wafer_slot = $Comment[$count]\n"; } elsif ($count==7) { print "Step_Value = $Comment[$count]\n"; } } } case (/Code/i) {print "Code = $array[1]\n";} case (/UserCode/i) {print "Code = $array[1]\n";} } } } sub Scan_File { my $dbh = DBI->connect('DBI:ODBC:mwalarm') or die "Couldn't connect to database: " . DBI->errstr; my $sql = "INSERT INTO Alarm_Info(EquipmentID, Chamber, LotID, Wafer_Flow, R +ecipe, Wafer Slot, AlarmType, Sensor, Signal_level, Alarm_Upper_Limit, Alarm_Lower_Limit, UserCode, Code +, Date_Submited, Time_Submited, False Positive) VALUES () "; my $sth = $dbh->prepare($sql) or die "Couldn't prepare statement: " . $dbh->errstr; $sth->execute() or die "cant execute" . $dbh->errstr; $sth->finish; }
I'm trying to figure out how to populate the databse with content from the Read_File sub. And how can i integrate the Scan_File sub into my program so that instead of those print line in the Read_File sub it populate the databse.

In reply to Parsing Script by phimtau123

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.