I have a script that reads a file and puts the data into my log database. The log file data is setup as KEY=>VALUE and as I read it I split it and put it in a hash array called sqlinsert.

I prepare the insert query in advance using this code:

my $fields = join(', ', @dbfields); my $places = join(', ', ('?') x @dbfields); my $insertrow = "INSERT into $table_webreporting ($fields) values +($places)"; $insertrow = $dbhc->prepare($insertrow);

I then insert it with this code:

    $insertrow->execute(@sqlinsert{@dbfields});

Works fine, untill recently when my log files were changed. In the new log file, each line of the log file doesn't always have all the fields. It only has the ones need for that transaction. This present a problem since I now have empty values. I tried setting up a foreach rounting like this hoping setting undef for the missing values would work, but perl complains when it does the excute about stuff not being defined:

foreach my $temp (@dbfields) { $sqlinsert{$temp} = undef unless ($sqlinsert{$temp}); }

What I like it to do is put a null value in for any field that doesn't have a value.


In reply to DBI and Hash by elmic11111

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.