fellow monks, something's bothering me these days.
what i have:
  • a csv-file (put into an array)
  • a mysql-table (fetched all into hash_ref)

    what i want to do:
  • update elements b and c of the csv-array with values of the mysql-hash_ref where element a of the csv-array matches the key of the mysql-hash_ref.
  • if element a does not match, update element b and c where element b of the csv-array matches the key of my_other_mysql-hash_ref have a look at the code:
    my $sth = $dbh->prepare("SELECT PlzOrt, Ortsbezeichnung, PlzGemein +de, GemeindeId FROM ortzugemeinde"); $sth->execute; my $hash_ref_ort = $sth->fetchall_hashref('Ortsbezeichnung'); $sth->execute; my $hash_ref_plz = $sth->fetchall_hashref('PlzOrt'); $sth = $dbh->prepare( q{ INSERT INTO clk_clicks (clk_year, clk_month, obj_id, own_id, cl +k_percent, clk_days, clk_plz, gde_name, clk_price, prc_id, obt_id, cl +k_total, clk_object, clk_detail, clk_pdf, clk_direct, clk_vmail, clk_ +fmail, gde_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? +, ?, ?,?) }); my @content; my @table; open FH, "< mydir/ImportFile_200201.txt" or die "Unable to open fi +le: $!"; while (<FH>) { chomp; @content = split /;/; if (exists $hash_ref_ort->{'$content[7]'}) { $content[18] = $hash_ref_ort->{'$content[7]'}->{'GemeindeI +d'}; $content[6] = $hash_ref_ort->{'$content[7]'}->{'PlzGemeind +e'}; } elsif (exists $hash_ref_plz->{$content[6]}) { $content[18] = $hash_ref_plz->{$content[6]}->{'GemeindeId' +}; $content[6] = $hash_ref_plz->{$content[6]}->{'PlzGemeinde' +}; } else { $content[18] = 0; } $sth->execute(@content); push @table, [@content]; } close FH; $dbh->commit; $dbh->disconnect; print "ok<br>";

    one line of the csv-file looks like:
    2002;01;62;6;1;31;5034;Suhr;3;435000;5;14;13;1;0;0;0;0

    this means: i want to append another value at EOL if 'Suhr' exists in $hash_ref_ort. if not i want to append another value at EOL if '5034' exists in $hash_ref_plz. if not, i want to append a 0 at EOL.

    note: i haven't fully optimized the code yet.

    it's basically something like this:
    UPDATE table1 INNER JOIN table 2 ON table1.ort = table2.ortsbezeichnung
    SET table1.gde_id = table2.gde_id
    WHERE table2.ortsbezeichnung = $ort;

    (but it seems that mysql does not support multi-table-updates, does it. )

    now what bothers me:
    this code behaves strange: usually it inserts 18 records into the db and gets stuck. some few times it inserted all the records into the db. some other times it does not do anything. i tried to uncomment line after line but this does not really change anything. and, oh yes, i tried it on another machine too.
    the thing is running (should i say 'walking'?) on win2k with apache 1.3.26.

    thank you all for your comments. i've run out of ideas to solve the problem since i do not have any ideas where to start to solve it.

    update: i'm wondering: the keys in the hash are i.e.
    {'Erlinsbach'}
    {'Niedererlinsbach'}
    as far as i know they should be unique anyway? even though they do contain the same letters? otherwise i'd be consternated...

    update II:
    turn out it's not a problem with the code. since i thought it's the code i posted here...
    i'm not sure WHAT it is, but i tried it (the same script) on my MacOSX-powerbook and it runs smothly.
    since the server i developed it, isn't a real server but a client (as my HW-geek told me today), my thoughts go into overflow of maximum connections.

    i'm just happy it's not my code... :) thanks anyway...

    In reply to updating array with matching value of hash by zetetes

    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.