FAQ
I would like to replace string 1 with string 2 in file3.dat, here is
what I
did:

#!/usr/bin/perl
@inputfile= ("string1"); # need to have an arrary later
@outputfile=("string2");
@attfile=("file3.dat");

$mytempfile=$inputfile[0];
$youtempfile=$outputfile[0];
$hisfile=$attfile[0];

perl -pi -e 's/$mytempfile/$youtempfile/' $hisfile;

The program reports an error:

Scalar found where operator expected at test line 10 near 's/
$mytempfile/$
youtempfile/' $hisfile
missing operator before $hisfile?)
Syntax error at test line 10, near "pi -e"

I am new to perl, please help!!!

thanks.

Search Discussions

  • Boxingcat at Nov 1, 2007 at 6:26 pm
    / Sorry if it is a repost. didn't see the first one in the group.


    I would like to replace string 1 with string 2 in file3.dat, here is
    what I
    did:

    #!/usr/bin/perl
    @inputfile= ("string1"); # need to have an arrary later
    @outputfile=("string2");
    @attfile=("file3.dat");

    $mytempfile=$inputfile[0];
    $youtempfile=$outputfile[0];
    $hisfile=$attfile[0];

    perl -pi -e 's/$mytempfile/$youtempfile/' $hisfile;

    The program reports an error:

    Scalar found where operator expected at test line 10 near 's/
    $mytempfile/$
    youtempfile/' $hisfile
    missing operator before $hisfile?)
    Syntax error at test line 10, near "pi -e"

    I am new to perl, please help!!!

    thanks.
  • John W . Krahn at Nov 1, 2007 at 8:23 pm

    On Thursday 01 November 2007 11:25, boxingcat wrote:
    / Sorry if it is a repost. didn't see the first one in the group.


    I would like to replace string 1 with string 2 in file3.dat, here is
    what I did:

    #!/usr/bin/perl
    use warnings;
    use strict;

    @inputfile= ("string1"); # need to have an arrary later
    @outputfile=("string2");
    @attfile=("file3.dat");

    $mytempfile=$inputfile[0];
    $youtempfile=$outputfile[0];
    $hisfile=$attfile[0];

    perl -pi -e 's/$mytempfile/$youtempfile/' $hisfile;
    You are trying to run an external program in perl (this is not a shell
    script.)

    perldoc -f system
    perldoc -f qx
    perldoc -f open


    However you can do what you want directly in perl:

    { local ( $^I, @ARGV ) = ( '', $attfile[ 0 ] );
    while ( <> ) {
    s/$inputfile[0]/$outputfile[0]/;
    print;
    }
    }




    John
    --
    use Perl;
    program
    fulfillment

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedNov 1, '07 at 5:57p
activeNov 1, '07 at 8:23p
posts3
users2
websiteperl.org

2 users in discussion

Boxingcat: 2 posts John W . Krahn: 1 post

People

Translate

site design / logo © 2022 Grokbase