
KevinR
Veteran

May 29, 2009, 8:56 AM
Post #4 of 11
(725 views)
|
|
Re: [w020637] Convert dev to uat
[In reply to]
|
Can't Post
|
|
#!/bin/perl use strict; use warnings; print qq{Finding "$find" and replacing it with "$replace"\n}; my $filename = '/test/perl/test.jil'; my $filename1 = '/test/perl/new_test.jil'; my $filename3 = '/test/perl/pairs.txt'; my %dict; #the pairs.txt is the file which contains key value combinations. open(my $DICTFILE, "<", $filename3) or die "Can't open $filename3: $!"; while(<$DICTFILE>){ chomp; my ($key, $val) = split /=/; $dict{$key} .= $val; } close $DICTFILE; open(my $IN, "<", $filename) or die "Can't open $filename: $!"; open(my $OUT,">", $filename1) or die "Can't open $filename1: $!"; while( my $line = <$IN> ){ foreach my $s (keys %dict){ $line =~ s/$s/$dict{$s}/g; } print $OUT $line; } close($IN); close($OUT); print 'Finished'; Use the "warnings" pragam instead of the -w switch. Use paired brackets with the quoting operators (like qq{}) and perl will escape the brackets inside the string. If you use a single symbol delimiter (like qq~~) and it appears in the string, you have to manually escape it. Use the three agrument form of open() . Include $! in the results returned from "die". Use indirect-filehandles ($IN instead of IN). Use the proper quotes: single-quotes for strings with no interpolation and double-quotes for strings with varaibles or meta sequences that need interpolation. Use "or" instead of "||" for program flow control. || has a higher precedence than "or". Don't needlessly quote varaibles. for example: There is no reason to put double-quotes around $line. You use quotes to make strings, in this case you are not making a string, just printing a scalar variable. This line looks wrong:
$dict{$key} .= exists $dict{$key} ? "$val" : $val; You needlessly quote $val if the condition is true, but even if the condition is false, you still concatenate $val to $dict{$key}, to me it look slike it should be written like so: -------------------------------------------------
|