You might want to try something like....
#!/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..

$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);
Then you while thing would do something like...
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


In reply to Re: Variable number of words/fields in a line/record by eejack
in thread Variable number of words/fields in a line/record by Tuna

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.