
rork
User
Apr 20, 2005, 9:04 AM
Post #2 of 3
(563 views)
|
|
Re: [cyberjoe] 'Processing a text file using a separate Regex file' help needed please
[In reply to]
|
Can't Post
|
|
The first problem is to read the filenames from the command line.
my $regexp_file = shift; my $text_file = shift; I would make a regexp file that looks like: can=cn\n get=gt\n .... Then itter over the file storing everything in a hash. my %regexps;
open (REGEXP, "<", $regexp_file); while(<REGEXP>) { chomp $_; my ($original, $new) = split(/=/, $_); $regexps{$orignal} = $new; } Open the text file and read it into an array. Itter through the array while executing the regexps and write it to a file.
open(FILE2, ">", "file2.txt"); foreach my $line(@file1) { foreach my $key(keys %regexps) { $line =~ s/$key/$regexps{$key}/g; } print FILE2 $line; } close(FILE2); Something like this should work but there might be better ways to do this. -- Don't reinvent the wheel, use it, abuse it or hack it.
|