better late than never...

[EMAIL PROTECTED] wrote on 03/11/2006 03:05:10 
PM:
> Today's Topics:
>    4. Removing the blank spaces (Naresh Bajaj)
> ----------------------------------------------------------------------
> ------------------------------
> Message: 4
> Date: Sat, 11 Mar 2006 13:49:20 -0600
> From: "Naresh Bajaj" <[EMAIL PROTECTED]>
> Subject: Removing the blank spaces
> To: [email protected]
> Message-ID:
>    <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hello,
> This is my problem. I have extracted one variable value from a file and
> saved it another fie.
> Problem is that it has too many spaces as shown in this example. I want 
to
> remove those blank spaces.
> If I use split, /  / $fti, I am getting partial results as shown below.
> Please let me know how can I remove those spaces.  I appreciated.
<examples removed>
split creates an array on a boundary. i think that while it could be used 
for what you want, it would be so in a round-about way. there are more 
direct methods. I suspect your dislike of the result is a product of 
not-enough-understanding (similar to the too little information is worse 
than none that creates panic among plebs) of split. instead of trying to 
deduce your code i'll give you a regexp that should give the desired 
results
[code]
#! /usr/bin/perl
$input="  example    information in     a string   ";
$input =~ /\s+/ /g;
print "$input\n";
$input =~ /^\s*(\S.*)\s*$/$1/;
print "$input\n";
[/code]
should print:
 example information in a string 
example information in a string
note that the first one has an extra " " at the end. it could also have 
more \n than intended. chomp removes that. i'm not sure, and don't believe 
it would remove leading white space. to remove that, i used the second 
substitute instead. 

> 
> I hope I clearly explained the problem. Please let me know if you are 
not
> clear about my issue. Thanks,

you did, and the potential of the confusion over what split does is why 
i'm now going to add a little explanation of what the regular expressions 
are doing. in the hopes that i'll help teach you them. =o)

/\s+/ /g

uses perl short \s which is [ \n\r\t] and one other thing also 
"whitespace." + means "1 or more" so it'll find the first run of white 
space and replace it with the next part, a single " ". the g makes this 
global, so it is don through out the whole set of data, hitting all the 
occurrences.

/^\s*(\S.*)\s*$/$1/

again uses the \s short and also uses the \S short. the \S means [^\s] and 
the . is anything, the * means 0 or more. the $ at the end is an 
end-of-line anchor and the ^ at the beginning is a beginning-of-line 
anchor
this finds all the white space until the first non-whitespace, then all 
the white space at the end. it then replaces the entire line with $! which 
is the capture from (\S.*) which is everything that is not the beginning 
and ending white space.
 
> --
> Naresh Bajaj, Intern,
> Cardiac Rhythm Disease Management,
> Medtronic Inc.,
> 763-514-3799


HTH
Josh Perlmutter

-----------------------------------------
PLEASE NOTE: 
SeaChange International headquarters in Maynard, MA is moving!
Effective March 1, 2006, our new headquarters address will be:

SeaChange International 
50 Nagog Park 
Acton, MA 01720 USA 

All telephone numbers remain the same: 
Main Corporate Telephone: 978-897-0100 
Customer Service Telephone: 978-897-7300

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to