in reply to Simply Too Slow Find and Replace

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.