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

Discussion in 'Perl Misc' started by Shotoku Taishi, Jul 9, 2003.

  1. #!/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
    Shotoku Taishi, Jul 9, 2003
    #1
    1. Advertising

  2. Shotoku Taishi

    J. Gleixner Guest

    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";
    }
    J. Gleixner, Jul 9, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark McKay
    Replies:
    3
    Views:
    1,300
    Thomas Weidenfeller
    Jan 21, 2004
  2. Stefan Siegl
    Replies:
    1
    Views:
    949
    Marrow
    Jul 18, 2003
  3. Abby Lee
    Replies:
    5
    Views:
    375
    Abby Lee
    Aug 2, 2004
  4. Joan Interactive Bussiness
    Replies:
    2
    Views:
    112
    Kyle T. Jones
    Feb 18, 2010
  5. Replies:
    1
    Views:
    511
    Rainer Weikusat
    Jun 21, 2012
Loading...

Share This Page