kulls has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,
I have a sample file like,
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

Replies are listed 'Best First'.
Re: parsing a file
by McDarren (Abbot) on Feb 02, 2006 at 06:42 UTC
    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 (<DATA>) { 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

    Update: Actually, don't do that - it's bad because it doesn't use placeholders. Here is a better method, following from the example given in the DBI recipes Tutorials.

    my $fieldlist = join(",", @fields); my $placeholders = join(",", map {'?'} @fields); my $insert = qq{ INSERT INTO foo ($fieldlist) VALUES ($placeholders)};
    You would then pass your @data list to db->execute, eg $dbh->execute(@data);

    Cheers,
    Darren :)

Re: parsing a file
by perlsen (Chaplain) on Feb 02, 2006 at 06:33 UTC
    Hi Kulls,
    Can you show me the database structure format which you are going to store the data from the file?.
    that is show me some example columns and data format, this would help us to give suggestions.
    Thanks,
    perlsen
Re: parsing a file
by Anonymous Monk on Feb 02, 2006 at 09:14 UTC
    How about a hash with col* as keys and data_line* as an array stored for the corresponding key?
    Well lets see what to do...
    my %database; my $curCol; for(<DATASET>) { if(/^(col\d+)\s+(.*)/) #(.+) isnt good but as I dont know exactlly +how your data looks like... { push(@{$database{$1}}, $2); $curCol = $1; } elsif(/^\s+(.*)/) { push(@{$database{$curCol}}, $1); } }
    Maybe I also expalin a little bit, but I didnt try out the code so there might be some bugs in it, I didnt notice. You create a hash of arrays with col as key and push each line that has no new col into the previous col. If there is a new col a new key and array is initiated and so on. I hope it is logical and helpfull.