All my comments are in the.. uh, comments. In the code. *grin*
#*********************** # Perl ActiveX Script #*********************** use Win32; #allows for MsgBox functionality use strict; ## <---- get in the habit <----- ## "die MsgBox()" works, but it's more of a coincidence; ## this is better sub msgbox_die { my $problem = shift; MsgBox $problem; die $problem; } ****************************************** sub Main() { my $file1 = "c:/foo/sub_list.txt"; my $file2 = "c:/foo/cust_names_city.txt"; ## you don't want the > in the filename - just a matter of cleanliness my $file3 = "c:/foo/cust_names_city_new.txt"; my @search; my @replace; open (SUBLIST, $file1) || msgbox_die("Can't open file"); ## fetch only one line at a time rather than slurping the entire file +at once while(<SUBLIST>) { ## use a third parameter in split: ## specifies how in how many parts we want to break the string ## split() can quit looking after that number, ## and it also prevents loss of data my ($key,$value) = split (/=/, $_, 2); chomp ($value); push @search, qx/\b\Q$key/; ## put a precompiled regex in the arra +y push @replace, $value; } close (SUBLIST); open(CUSTLIST, $file2) || msgbox_die("Can't open file"); open(NEWCUSTLIST, ">$file3") || msgbox_die("Can't open file"); ## you +want the > here while(<CUSTLIST>) { ## ditto slurping my $i = 0; for my $re (@search) { ## go through all the stored regexes, s/$re/$replace[$i]/ig; ## and apply them, $i++; ## keeping track of which string we are replacing with } print NEWCUSTLIST $_; ## no need to keep the result in memory } close (CUSTLIST); close (NEWCUSTLIST); return 0; # DTSTaskExecResult_Success; } ******************************************
____________
Makeshifts last the longest.

In reply to Re: Simply Too Slow Find and Replace by Aristotle
in thread Simply Too Slow Find and Replace by guopan

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.