Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How do I apply the search and replace routine so that it works?

Reply
Thread Tools

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

 
 
Shotoku Taishi
Guest
Posts: n/a
 
      07-09-2003
#!/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


 
Reply With Quote
 
 
 
 
J. Gleixner
Guest
Posts: n/a
 
      07-09-2003
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";
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
| SEO , Search Engine Optimizer, SEARCH OPtiMIzAtIoN with SeaRch OPtiMizer optimizer.seo@gmail.com Digital Photography 0 04-22-2007 04:20 AM
[XSLT] could not apply "apply-templates" Stefan Siegl XML 1 07-18-2003 09:43 AM
Defective Search Routine Unsigned Computer Support 2 07-11-2003 03:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57