Well, all of the discussion about benchmarks set me to wondering which was the most efficient. So here are the actual results. Bear in mind that the benchmarks were only repeated twice and it is a working server. The times where obtained by...
$ticks = time(); .... $ticks = time() - $ticks; print "$count Records updated in $ticks seconds\n";
The program is moving data from one MySQL database to another. The subroutine gets passed a hash_ref to a map and a hash_ref from the source database as returned by $query->fetchrow_hashref. The map looks like...
my(%map) = qq( 'AMNT_PAID', '0.0', 'AMOUNT', '{PurchAmt}', 'DELIVERY_NOTE', '"{Delivery}"', 'EXPORTED', 'IF({Exported}, "Y", "N")', 'FUNDS', 'IF("{CompanyID}"="BCU", "USD" +, "CAD")');
The makemap sub returns a string that can be used in the SET portion of an INSERT sql statement. The code snippet follows.
# Create a set list from the data and the data map sub makemap { my($map, $r) = @_; my($key, $result, $t, %h, $ndx); %h=%$r; $result=""; foreach $key (sort(keys(%$map))) { #print "$key:\t$map->{$key}\n"; $t = $map->{$key}; # substitute the actual value for the placeholder fieldname $t =~ s/(\{\S+\})/defined(eval("\$h$1")) ? eval("\$h$1") : "NULL +"/eg; # replace any double quotes with escaped doubles if ((substr($t,0,1) eq '"') && (length($t) > 2)) { # best 196 sec # $ndx = 1; # while( ($ndx = index($t, '"', $ndx)) != -1 and ++$ndx < len +gth($t)) { # substr($t, $ndx++, 0, '"'); # } # original 198 sec # for ($ndx=1; $ndx < length($t)-1; $ndx++) { # if (substr($t,$ndx,1) eq '"') { # substr($t,$ndx,1) = '""'; # $ndx++; # } # } # substring 204 sec # substr($t, 1, -1) =~ s/"/""/g; # zero width assertion 201 sec $t =~ s/(?<=.)"(?=.)/""/g; } #print "$t\n"; $result .= qq($key=$t ,); } chop($result); # remove trailing , return($result); }
Averaged over two runs each with 17400 records with 750 "pathological" records (with embedded double quotes)the results seem to indicate the iterative method is faster than the regex way but I like the elegance of the regular expression.

In reply to Re: Re: Regex matching - Actual code and benchmarks - on anywhere but the beginning or end of a line by Dr.dos
in thread Regex matching on anywhere but the beginning or end of a line by Anonymous Monk

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.