On Friday 28 May 2004 03:31 pm, Mandar Rahurkar wrote:
> Hi,
>    I am trying to remove from file :
>         1. all characters but any alphabet and numbers.
>         2. all trailing spaces should be made to one space.
>
>    following code doesnt seem to work for objective [1] mentioned above.
> Can anyone please point out why ?
>
> Thanks,
> Mandar
> ---
> open(fp,$file) or die "Cant open $file :$!\n";
> @cont=<fp>;
> [EMAIL PROTECTED];
>
> for(@cont) {
>   tr/A-Z/a-z/;

You forgot the 'g':
   tr/A-Z/a-z/g;
(but that will remove ALL spaces too!)

>   s/^[a-z0-9]/ /;
>   s/\s+/ /g;
> }
> ---

Hi Mandar -

Please restate your goals.

 Do yYou you want to remove spaces too?
 What about nls and tabs?
 Do you want to lowercase everthing (what your script does now)?

Assume you want to  to remove from file :
    1. all characters but any alphabet and numbers and whitespace.
    2. all non nl whitespace should be made to one space.
    3. rewrite the file.

Then:

#----
# open for update (using caps for handle as per normal convention).
open( FP, "+< $file" ) or die "Cant open $file :$!\n";
# 'slurp" file to scalar (avoids having to 'for' thru array
$_ = do { local $/; <FP>; };

# remove non alphebetics
s/[^A-Za-z0-9\s]//sg;
# change non-nl whitespace to one space
s/[\t ]+/ /sg;

# seek to start, rewrite, and close
seek FP, 0, 0;
print FP $_;
close FP;
#----

Aloha => Beau;


-- 
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