[EMAIL PROTECTED] wrote:
With regards to the script below, inside the foreach loop, can someone explain to me why the expression $_=~ s/\nfred\n/nancy/; did not change the default variable $_ from fred (enclosed by \n) to nancy.

Because there is no $_ variable with two newlines. @newdata contains the two elements "hello how are you\n" and "fred\n", and you are dealing with one element at a time.

my @newdata = <MYDATA>;

Instead of storing the data in an array, slurp them into a scalar variable.

    my $newdata;
    {
        local $/;
        $newdata = <MYDATA>;
    }

foreach (@newdata){
    $_=~ s/\nfred\n/nancy/;
    print MYDATA "$_";
    print "$_";
}

    $newdata =~ s/\nfred\n/ nancy\n/;
    print MYDATA $newdata;
    print $newdata;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to