in reply to Re^3: Flat file handling
in thread Flat file handling
#! perl -w scipt use strict; use warnings; use DBI; use DBD::ODBC; use File::Copy; my ($data_source, $database, $user_id, $password) = qw( *********** ** +******** ********** ******* ); my $conn_string = "driver={SQL Server}; Server=$data_source; Database= +$database; Trusted_Connection=yes"; my $dbh = DBI->connect( "DBI:ODBC:$conn_string" ) or die $DBI::errstr; my $Request_id; my $Variable_number; my $Variable; my $Choice; my @VARIABLE; my @CHOICE; my $Command; my $Return_results; my $Component_number_old = 1; my $Component_number = 1; my $SPROC; my $unique; my $elements_in_array; my $flat_file = "Flat_file.txt"; open (FLAT_FILE, "<$flat_file"); while (<FLAT_FILE>){ chomp; $Component_number_old = $Component_number; if ($_ =~ /SPROC\sName\:\s([a-z|A-Z|0-9|_]{1,100})/){ print "Hi_A\n"; $SPROC = $1; } elsif ($_ =~ /^\*(.{36})\;(\d{1,5})\;(\d{1,5})\;([a-z|A-Z|0-9|_]{1,5 +0})\;(.{1,100})/){ print "$_\n"; push (@VARIABLE, $Variable); push (@CHOICE, $Choice); print "Hi_B\n"; $unique = $1; $Component_number = $2; $elements_in_array = $3; $Variable = $4; $Choice = $5; } if ($_ =~ /^\sEND/){ print "Hi_E\n"; push (@VARIABLE, $Variable); push (@CHOICE, $Choice); $Command = join(' ', 'EXEC', $SPROC, join(', ', @CHOICE[1 .. $elements_in_array])) . ';'; my $Request_id = $CHOICE[26]; print "$Command\n"; Got_Command($Command, $Request_id); undef @CHOICE; my @CHOICE; } } close FLAT_FILE; my $source = $flat_file; my $dest = 'Bin'; move($source, $dest) or die "Error moving file: $!\n"; sub Got_Command { my($Command,$Request_id) = @_; my $output_file = "Output/Output_file_".$Request_id.".txt"; open (OUTPUT_FILE, "+>>$output_file"); print "$Command\n"; my $sthB_A = $dbh->prepare($Command) or die "Couldn't prepare query +: ".$dbh->errstr; $sthB_A->execute() or die "Couldn't execute query: ".$sthB_A->errstr +; my $Return_results = "Select * from Result_storage_keep where Unique +_identifier = ".$Request_id."\;"; print "$Return_results\n"; my $sth = $dbh->prepare($Return_results) or die "Couldn't prepare qu +ery: ".$dbh->errstr; $sth->execute() or die "Couldn't execute query: ".$sth->errstr; while (my @row = $sth->fetchrow_array ) { print OUTPUT_FILE join("\t", @row); print OUTPUT_FILE "\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Flat file handling
by Hena (Friar) on Nov 19, 2004 at 13:26 UTC |