in reply to Variable number of words/fields in a line/record
#!/usr/bin/perl -w use strict; my $in = "/home/trix/test"; my $out = "/home/trix/out"; open IN, "$in" || die "$!\n"; open OUT, ">$out" || die "$!\n"; while (<IN>){ chop; my (@temp_array) = split( /\s/, $_); if ($#temp_array == 9){ #10 elements my @keeper_array = @temp_array[3 .. 9]; } else { # 9 elements my @keeper_array = (@temp_array[3 .. 7], "whatever your filler + is", $temp_array[8]); } }
Update Misunderstood the basic question...
But let's assume you are using dbi for a moment... You might have something like..
Then you while thing would do something like...$sql_statement = qq|insert into log_table (field1, field2, field3, field4, field5, field6) VALUES (?,?,?,?,?,?)|; $sth{'6'} = $dbh->prepare($sql_statement); $sql_statement = qq|insert into log_table (field1, field2, field3, field4, field5, field6, field7) VALUES (?,?,?,?,?,?,?)|; $sth{'7'} = $dbh->prepare($sql_statement); $sql_statement = qq|insert into log_table (field1, field2, field3, field4, field5, field6, field7, field8) VALUES (?,?,?,?,?,?,?,?)|; $sth{'8'} = $dbh->prepare($sql_statement);
while (<IN>){ chop; my ($waste, $wastea, @temp_array) = split; my $rv = $sth{$#temp_array+1}->execute(@temp_array); }
Regardless of how you do it, you would need to be able to predict *something* about the incoming data either based on the number of elements or on something you could match in the data.
EEjack
|
|---|