col1 data_line1
col2 data_line2
col3 data_line3
data_line4
data_line5
data_line6
col4 data_line7
I need to parse the file in perl, so that i can store the data_lines in the database tables which is having a column col1, col2, col3 and so on.
Can anyone suggest me, how to proceed with this ?
- kulls
There are lots of ways that you could do this. But firstly, you didn't say what you want to do with data_line[4|5|6]?
But anyway, lets assume that you want to ignore them. Then you could do something like:
use strict;
my (@fields, @data);
while () {
chomp;
my ($field, $value) = split;
next if !defined $value;
push @fields, $field;
push @data, $value;
}
my $insert = "INSERT INTO foo ("
. join(",", @fields)
. ") VALUES ("
. join(",", map { qq("$_") } @data)
. ");";
print "$insert\n";
__DATA__
col1 data_line1
col2 data_line2
col3 data_line3
data_line4
data_line5
data_line6
col4 data_line7
Cheers,
Darren :)
perlmonks.org content © perlmonks.org and Anonymous Monk, kulls, McDarren, perlsen
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03