Could you give an example of using grep on a array to do a replace?

On Wed, 24 Nov 2004 10:05:39 +0530, Prasanna Kothari wrote:

>Hi,
>Replace
>$output = ~ s/AAA*?BBBt/AAA 111 *?222 BBB/g;
>with
>my $output =~ s/AAA*?BBBt/AAA 111 *?222 BBB/g;
>
>You can use "perl -c <filename>" which will show you such type of errors.
>
>After opening the output file, it's better to check if the file is
>opened successfully(as done in the case of INPUT).
>
>I think there's something wrong with the logic.
>The INPUT file is opened, all it's contents are in the array
>@match_lines, and then after doing a pattern match, you are just
>printing the values to the output file.
>Your script has not replaced anything.
>Use grep function to replace all the contents of an array and write the
>array back to the output file.
>
>
>--Prasanna
>FlashMX wrote:
>
>>Hi,
>>
>>The code below opens a file and writes the contents to a temporary file. I 
>>need to do a search and replace of certain matches in the files and I thought 
>>I might be able to use regular
>>expressions.
>>Being new to perl and now trying expressions has almost put me over the edge.
>>
>>When I run the below code I get this error:
>>
>>Global symbol “output” requires explicit package name at filename.pl line 
>>18....
>>
>>I’m not sure whats wrong by the error.
>>
>>#!/usr/local/bin/perl
>>use strict ;
>>
>>my $ofile = "file.out";
>>my $ifile = "file.in";
>>
>>open(INPUT, $ifile) or die "Can't open input file: $ifile ($!)" ;
>>      my @match_lines = <INPUT>;
>>close ( INPUT );
>>
>>
>>open( OUTPUT, "> $ofile" );
>>
>>      $output = ~ s/AAA*?BBBt/AAA 111 *?222 BBB/g;
>>
>>      print OUTPUT @match_lines;
>>close ( OUTPUT );
>>
>>#rename($ofile, $ifile) ;
>>
>>
>>
>>
>>
>>
>>
>
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
><http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>




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


Reply via email to