|
In a message dated 3/11/2006 6:14:38 P.M. Eastern Standard Time,
[EMAIL PROTECTED] writes:
> ----- Original Message -----
> From: "Naresh Bajaj" <[EMAIL PROTECTED]> > To: <[email protected]> > Sent: Saturday, March 11, 2006 8:49 PM > Subject: Removing the blank spaces > > > 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. > > Example: > > Here is the output saved file looks like: > > ! fti = > > 240 > > Use regexp. > $x = "a b c d\n12 3\n\nA B C"; > $x =~ s/\s+/ /sg; > $x =~ s/^\s*(.*?)\s*$/$1/s; > print ":$x:; > >:a b c d 12 3 A B C: > > Petr Vileta, Czech republic naresh --
also try:
join ' ', split ' ', $string; # ' ' is
quote-space-quote
it may be faster if you're re-fornatting thousands of strings.
note that split ' ' (quote-space-quote) is magically different from split /
/ (slash-space-slash)
in that it ignores leading and trailing whitespace and splits on multiple
embedded ws
characters.
my $x = qq( a bbbb %^&* c d\n12
3\n\nA B C );
print qq(:$x: \n); $x = join ' ', split ' ', $x; print qq(:$x:); output:
: a bbbb %^&* c d 12 3 A B C :
:a bbbb %^&* c d 12 3 A B C: end output you can also use printf() to gain control over output
formatting:
my $x = qq( a bbbb %^&* c d\n12 3\n\nA
\n\n B C );
print qq(:$x: \n); printf ':my %s.%s is %s when %s/%s == %s/%s:', split ' ', $x; output:
: a bbbb %^&* c d 12 3 A
B C :
:my a.bbbb is %^&* when c/d == 12/3: end output hth -- bill walters
|
_______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
