How do I apply the search and replace routine so that it works?

S

Shotoku Taishi

#!/usr/bin/perl-w
$DIR = ".";
$hello = "hello.txt";

open(OUTPUT,">$DIR/$hello") or
die("Can't open $DIR/$hello : $!\n");
my @a = qw(
hello.exp
hallo.exp
Hola.exp
Ciao.exp
);
foreach $a (@a) {
print OUTPUT <<EOT;
file=$a log=$a\.log
EOT
}

The above generates a file hello.txt with the following output:
file=hello.exp log=hello.exp.log
file=hallo.exp log=hallo.exp.log
file=Hola.exp log=Hola.exp.log
file=Ciao.exp log=Ciao.exp.log

but I would like the output to be

file=hello.exp log=hello.log
file=hallo.exp log=hallo.log
file=Hola.exp log=Hola.log
file=Ciao.exp log=Ciao.log

How would I do this using the simple search and replace routine?

I have tried
s/\.exp\./\./g;
but the output does not seem to change.

How do I apply the search and replace routine so that it works?

Thank you in advance.

Michael
 
J

J. Gleixner

Shotoku Taishi wrote:
[...]
foreach $a (@a) {
print OUTPUT <<EOT;
file=$a log=$a\.log
EOT
} [...]
I have tried
s/\.exp\./\./g;
but the output does not seem to change.

How do I apply the search and replace routine so that it works?

Need to apply your substitution on something, using =~, otherwise it
only operates on $_.

foreach my $a (@a)
{
my $log = $a;
$log =~ s/\.exp\./\./;

print OUTPUT "file=$a log=$log.log\n";
}

foreach (@a)
{
print OUTPUT "file=$_ ";
s/\.exp\./\./; # $_ is operated on
print OUTPUT "log=$_.log\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top