Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 7th, 2009, 11:07 AM
LRoberts LRoberts is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 8 LRoberts User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 1 m 10 sec
Reputation Power: 0
Unhappy Remove white space and carriage returns, line feeds

I have a perl script that reads a $message variable which is typed in by the user.

We was trying to remove all white spaces that could be in front, all white spaced at the end, extra spaced inbetween words (For example 2 spaces or more between works would be reduced to 1 space) and to remove any carriage returns or line feeds.

I am very new to Perl and have been unable to figure this out.

What I had so far was this....
$message =~ s/[\n\r\s]+//g;
But this is removing all white space even between words.

This is the input it was given...
ottocat:/lcl/apps/esm/bin>postEvent.pl -s 60 -m ' MESSAGE GOES HERE ' -g 'INFR_Apps' -i 'ISOC INSTRUCTIONS GO HERE' -t -p 'Test_Program' Testing

The -m is the message text.

However the output results in...
MESSAGEGOESHERE

It should say "MESSAGE GOES HERE"

Could somebody show me the correct format to use to remove....

1.) Front white spaces
2.) Carriage returns
3.) Line feeds
4.) End white spaces
5.) Any extra white spaces between words.
So for example if it said...
"This is a test."
It would remove the extra white space between "is" and "a".

Thank you

Reply With Quote
  #2  
Old October 7th, 2009, 11:32 AM
shawnhcorey's Avatar
shawnhcorey shawnhcorey is offline
wizard
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Location: The Great White North
Posts: 75 shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 18 h 51 m 11 sec
Reputation Power: 136
In three steps:
perl Code:
Original - perl Code
  1. $message =~ s/^\s+//;
  2. $message =~ s/\s+/ /g;
  3. $message =~ s/\s+$//;


PS this does not remove non-breaking spaces (ASCII code \xA0)
__________________
__END__

I love Perl; it's the only language where you can bless your thingy.

Reply With Quote
  #3  
Old October 7th, 2009, 11:35 AM
LRoberts LRoberts is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 8 LRoberts User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 1 m 10 sec
Reputation Power: 0
Thank you very much for the help.
Could you please tell me what each line is doing?
I really want to learn this.

Wow I see its going to take me a while to learn how to do this stuff.

Quote:
Originally Posted by shawnhcorey
In three steps:
perl Code:
Original - perl Code
  1. $message =~ s/^\s+//;
  2. $message =~ s/\s+/ /g;
  3. $message =~ s/\s+$//;


PS this does not remove non-breaking spaces (ASCII code \xA0)

Reply With Quote
  #4  
Old October 7th, 2009, 11:54 AM
LRoberts LRoberts is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 8 LRoberts User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 1 m 10 sec
Reputation Power: 0
Which part of these 3 steps is removing the Carriage returns and line feeds if any?

Reply With Quote
  #5  
Old October 7th, 2009, 12:04 PM
shawnhcorey's Avatar
shawnhcorey shawnhcorey is offline
wizard
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Location: The Great White North
Posts: 75 shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 18 h 51 m 11 sec
Reputation Power: 136
Quote:
Originally Posted by LRoberts
Thank you very much for the help.
Could you please tell me what each line is doing?
I really want to learn this.

Wow I see its going to take me a while to learn how to do this stuff.


1. $message =~ s/^\s+//; # removes leading white space

2. $message =~ s/\s+/ /g; # replaces sequence of white space with a single space

3. $message =~ s/\s+$//; # removes trailing white space

See:
http://perldoc.perl.org/perlretut.html
http://perldoc.perl.org/perlre.html

Reply With Quote
  #6  
Old October 7th, 2009, 12:07 PM
LRoberts LRoberts is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 8 LRoberts User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 1 m 10 sec
Reputation Power: 0
Ok thanks for the help.
In order to remove carriage returns and line feeds I did this...

# Remove Carriage returns
chomp $instructions;

# Remove Line Feeds
$message =~ s/\n//g;


Does that look correct?

Again thank you for the help.

Quote:
Originally Posted by shawnhcorey
1. $message =~ s/^\s+//; # removes leading white space

2. $message =~ s/\s+/ /g; # replaces sequence of white space with a single space

3. $message =~ s/\s+$//; # removes trailing white space

See:
http://perldoc.perl.org/perlretut.html
http://perldoc.perl.org/perlre.html

Reply With Quote
  #7  
Old October 7th, 2009, 12:24 PM
shawnhcorey's Avatar
shawnhcorey shawnhcorey is offline
wizard
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Location: The Great White North
Posts: 75 shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level)shawnhcorey User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 18 h 51 m 11 sec
Reputation Power: 136
Quote:
Originally Posted by LRoberts
Ok thanks for the help.
In order to remove carriage returns and line feeds I did this...

# Remove Carriage returns
chomp $instructions;

# Remove Line Feeds
$message =~ s/\n//g;


Does that look correct?

Again thank you for the help.


chomp() removes the last occurrence of the contents of $/ from the end of a string. See http://perldoc.perl.org/functions/chomp.html

s/\n//g removes all LF characters

s/\s+$// removes all white space from the end of a string. White space is defined as \n \r \x20 \t \f See http://perldoc.perl.org/perlre.html and http://perldoc.perl.org/perlretut.html

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Remove white space and carriage returns, line feeds

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap