Home > Archive > PERL Beginners > January 2006 > Simplest Way
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Gladstone Daniel - dglads 2006-01-19, 6:58 pm |
| What would be the simplest way to remove leading and trailing blanks
from a user input?=20
Daniel Gladstone (Daniel.Gladstone@Acxiom.com)
****************************************
***********************************
The information contained in this communication is confidential, is
intended only for the use of the recipient named above, and may be legally
privileged.
If the reader of this message is not the intended recipient, you are
hereby notified that any dissemination, distribution or copying of this
communication is strictly prohibited.
If you have received this communication in error, please resend this
communication to the sender and delete the original message or any copy
of it from your computer system.
Thank You.
****************************************
************************************
| |
| Paul Lalli 2006-01-19, 6:58 pm |
| Gladstone Daniel - dglads wrote:
> What would be the simplest way to remove leading and trailing blanks
> from a user input?
The same way recommended in the Perl FAQ. Why haven't you read it
before asking thousands of people to read it to you?
perldoc -q space
http://perldoc.perl.org/perlfaq4.ht...-of-a-string%3F
Paul Lalli
| |
| Chas Owens 2006-01-19, 6:58 pm |
| On 1/19/06, Gladstone Daniel - dglads <Daniel.Gladstone@acxiom.com> wrote:
> What would be the simplest way to remove leading and trailing blanks
> from a user input?
>
> Daniel Gladstone (Daniel.Gladstone@Acxiom.com)
snip
One easy way to do it would be to use a regex:
#!/usr/bin/perl
$input =3D <>;
chomp($input);
print "[$input]\n";
$input =3D~ s/ #replace
^\s* #all whitespace at the front
(.*?) #all of the stuff in the middle
\s*$ #all whitespace at the end
/$1/x; #with the stuff in the middle
print "[$input]\n";
| |
| Paul Lalli 2006-01-19, 6:58 pm |
| Chas Owens wrote:
> On 1/19/06, Gladstone Daniel - dglads <Daniel.Gladstone@acxiom.com> wrote:
[color=darkred]
> One easy way to do it would be to use a regex:
> #!/usr/bin/perl
>
> $input = <>;
> chomp($input);
> print "[$input]\n";
> $input =~ s/ #replace
> ^\s* #all whitespace at the front
> (.*?) #all of the stuff in the middle
> \s*$ #all whitespace at the end
> /$1/x; #with the stuff in the middle
>
> print "[$input]\n";
Please look at the FAQ I posted to learn why you should *NOT* use this
solution.
One more time:
perldoc -q space
http://perldoc.perl.org/perlfaq4.ht...-of-a-string%3F
Paul Lalli
| |
| Bob Showalter 2006-01-19, 6:58 pm |
| Gladstone Daniel - dglads wrote:
> What would be the simplest way to remove leading and trailing blanks
> from a user input?
To remove just blanks (but preserving possible trailing newline):
s/^ +//, s/ +$// for $input;
To remove all whitespace:
s/^\s+//, s/\s+$// for $input;
I like this idiom, because you can easily trim several variables at once:
s/^\s+//, s/\s+$// for $foo, $bar, $baz;
s/^\s+//, s/\s+$// for @array;
s/^\s+//, s/\s+$// for values %hash;
| |
| usenet@DavidFilmer.com 2006-01-19, 6:58 pm |
| Paul Lalli wrote:
>http://perldoc.perl.org/perlfaq4.ht...-of-a-string%3F
The perldocs are correct and fine, of course, but if you want to do it
the "Damian Conway way" (from his book, Perl Best Practices) you would
use a syntax something like:
s{\A \s* | \s* \z}{}gxm;
Or maybe
use Regexp::Common qw /whitespace/;
s/$RE{ws}{crop}//;
Damian makes a good case for these techniques... it would be wise for
coders to consider his recommendations, even though (in this sort of
situation, at least) they are a rather severe departure from
long-established practices.
New coders should especially consider Damian's recommendations, so they
don't need to struggle with un-learning old (and not so good) habits.
| |
|
| hi there
correct me if i'm wrong, but can't you use a chomp or s/\w.*\\
ciao
| |
| Paul Lalli 2006-01-20, 6:59 pm |
| abhi wrote:
> correct me if i'm wrong, but can't you use a chomp or s/\w.*\\
You're wrong. :-)
chomp removes one single newline from the end of a string. It does not
remove any other whitespace, and it does not remove multiple whitepaces
(without a loop surrounding it, at least).
As for your attempt at a regexp, firstly that's a syntax error.
Secondly, it looks like what you meant was s/\w.*// which would find
the first word-character (ie, letter, number, or underscore) and remove
that word character as well as everything that follows it. I can't see
how that has anything to do with the topic at hand (removing leading
and trailing whitespace).
Paul Lalli
|
|
|
|
|