[EMAIL PROTECTED] wrote: > Hi, > 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. > > Thanks > > ######### start of script ################### > use strict;
Always use warnings; > open (MYDATA, ">BACCARATDATA.TXT") || DIE $!; Perl is case sensitive. DIE is a filehandle and you mean the function 'die'. It is much safer to use the three-argument version of open and indirect file handles: open my $mydata, '>', 'BACCARATDATA.TXT' or die $!; > print MYDATA "hello how are you\nfred\n"; > close MYDATA; > > open (MYDATA, "<BACCARATDATA.TXT") || DIE $!; > my @newdata = <MYDATA>; > close MYDATA; Here you are reading back the contents of the file you just wrote, with the default record separator of "\n". So @newdata consists of two elements: 0: "hello how are you\n" 1: "fred\n" > open (MYDATA, ">>BACCARATDATA.TXT") || DIE $!; > foreach (@newdata){ > $_=~ s/\nfred\n/nancy/; There is no need to mention $_ here - as you mentioned at the top of your post it is the default if you don't specify an object variable. But none of the lines contain "\nfred\n" as I showed above, so this substitute will never succeed. s/^fred$/nancy/; will do what you intended. > print MYDATA "$_"; > print "$_"; > } > close MYDATA; HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/