Adriano Allora wrote:
>
> Hi to all,
Hello,
> I need to substitute in a text a serie of word with another serie of
> word (for example: when I have the name I want the correct surname).
> Modifying a someone else script I wrote this one:
>
> #!/usr/bin/perl -w
>
> my $original = '(name1|name2)';
> my $new = '(surname1|surname2)';
>
> $file_name = "ciccio.txt";
>
> open (INPUT, $file_name);
>
> while (<INPUT>)
> {
> s/$original/$new/g;
> $cortext .= $_;
> }
>
> print $cortex;
>
> close (INPUT);
>
> (first)It doesn't work correctly (it substitutes each $original with
> the entire string "(surname1|surname2)"), and I'm not able to
> understand why.
> (second)Another thing: How can I write again, in a way more elegant,
> the part:
>
> $cortext .= $_;
> }
> print $cortex;
>
> for example having only
>
> }
> print;
#!/usr/bin/perl -w
use strict;
my %names = (
'name1' => 'surname1',
'name2' => 'surname2',
);
my $regex = qr/\b(@{[join '|', map "\Q$_", sort { $b cmp $a } keys %names]})\b/;
my $file_name = 'ciccio.txt';
open INPUT, $file_name or die "Cannot open $file_name: $!";
while ( <INPUT> ) {
s/$regex/$names{$1}/g;
print;
}
close INPUT;
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]