Re: removing white space
Mariusz K wrote: Hi, Hello, One part of my script ads several strings into one: $text = $part1.$part2.$part3.(...etc) However, if the part3 through part10 were empty I get that many white spaces at the end of $text. I thought the best thing would be just to remove the spaces at the end, but how? (maybe search and remove pattern?) Why not use an array instead of ten separate scalars? If the variable is really empty it wouldn't print out as a space. $ perl -e' ( $part1, $part2, $part3, $part4, $part5 ) = ( one, two, , undef, five ); $text = $part1.$part2.$part3.$part4.$part5; print $text\n; ' onetwofive How to remove whitespace at the end (or beginning) of a string is a Frequently Asked Question. perldoc -q How do I strip blank space from the beginning/end of a string John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: removing white space
Thank you for your response. I went to perldoc on the net and couldn't find any examples or more info on -q space Mariusz - Original Message - From: Sudarshan Raghavan [EMAIL PROTECTED] To: Perl beginners [EMAIL PROTECTED] Sent: Friday, November 22, 2002 1:29 AM Subject: Re: removing white space On Fri, 22 Nov 2002, Mariusz K wrote: Hi, One part of my script ads several strings into one: $text = $part1.$part2.$part3.(...etc) However, if the part3 through part10 were empty I get that many white spaces at the end of $text. I thought the best thing would be just to remove the spaces at the end, but how? (maybe search and remove pattern?) perldoc -q space -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: removing white space
Thanks for replying. My text data comes from text fields so I read them through param and then in one instance I combine them into one string. Therefore, I don't need to remove all white spaces - only the ones at the end. I tried to lookup the perldoc -q on the net but couldn't find anything? (www.perldoc.com right?) Mariusz - Original Message - From: John W. Krahn [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Friday, November 22, 2002 2:50 AM Subject: Re: removing white space Mariusz K wrote: Hi, Hello, One part of my script ads several strings into one: $text = $part1.$part2.$part3.(...etc) However, if the part3 through part10 were empty I get that many white spaces at the end of $text. I thought the best thing would be just to remove the spaces at the end, but how? (maybe search and remove pattern?) Why not use an array instead of ten separate scalars? If the variable is really empty it wouldn't print out as a space. $ perl -e' ( $part1, $part2, $part3, $part4, $part5 ) = ( one, two, , undef, five ); $text = $part1.$part2.$part3.$part4.$part5; print $text\n; ' onetwofive How to remove whitespace at the end (or beginning) of a string is a Frequently Asked Question. perldoc -q How do I strip blank space from the beginning/end of a string John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: removing white space
Mariusz wrote: Thank you for your response. I went to perldoc on the net and couldn't find any examples or more info on -q space Mariusz on your machine, try the following in the command line: perldoc -q space david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: removing white space
Mariusz wrote: From: John W. Krahn [EMAIL PROTECTED] How to remove whitespace at the end (or beginning) of a string is a Frequently Asked Question. perldoc -q How do I strip blank space from the beginning/end of a string Thanks for replying. My text data comes from text fields so I read them through param and then in one instance I combine them into one string. Therefore, I don't need to remove all white spaces - only the ones at the end. I tried to lookup the perldoc -q on the net but couldn't find anything? (www.perldoc.com right?) perldoc is a program that is installed on your computer when you install Perl. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: removing white space
I tried to lookup the perldoc -q on the net but couldn't find anything? (www.perldoc.com right?) perldoc is software that generally is installed with perl. perldoc.com has the same information, but doesn't have a way to use the flags that the software version has. The -q flag to perldoc searches the FAQs, so from www.perldoc.com, select the FAQ index. Using your browser to search for the string space on that page leads to How do I strip blank space from the beginning/end of a string? within perlfaq4. If you click on the perlfaq4 link, and again search on space, you get a link to that question, clicking on which gives you http://www.perldoc.com/perl5.8.0/pod/perlfaq4.html#How-do-I-strip-blank-spa ce-from-the-beginning-end-of-a-string- which I won't copy to hear because everyone should be able to find it now on their own if they want to read it. /\/\ark -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: removing white space
On Fri, 22 Nov 2002, Mariusz K wrote: Hi, One part of my script ads several strings into one: $text = $part1.$part2.$part3.(...etc) However, if the part3 through part10 were empty I get that many white spaces at the end of $text. I thought the best thing would be just to remove the spaces at the end, but how? (maybe search and remove pattern?) perldoc -q space -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: removing white space revisited
That is kool, but I am using CGI.pm like we all should be :) On Monday, February 18, 2002 at 2:01 PM, [EMAIL PROTECTED] (John M) wrote: David, foreach $key (keys(%in)) { $in{$key} =~ s/^\s+//; $in{$key} =~ s/\s+$//; } That's all you have to do. -jdm (%in is generated under cgi-lib.pl) So back to my question do need to assign param() to hash? Thx, Dave From an earlier post: This is great for 'Upper-casing' just the first letter of a string! #!/usr/bin/perl -w my $key =today's Departure date; $key =~ s/([\w'-]+)/\u$1/g; print $key; From: David Gilden [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: removing white space revisited Date: Mon, 18 Feb 2002 13:48:46 -0500 Hi, My goal is to clean up the fields submitted from a form and then be able to use them in other places. Does the following modify the value of param('name') -- if I have a form element named 'name' -- permanently (like when you have an array) foreach my $key (param()){ $val = param($key), $value =~ s/^\s+//; $value =~ s/\s+$//; $key =~ s/\d$//; print $key: $val\n\n; } later in the script print param('email') # will this have been cleaned regrex in the up the loop above? Do I have to assign my 'cleaned' data to a hash? Thanks! Dave ** * Cora Connection Your West African Music Source * * http://www.coraconnection.com/ * * Resources, Recordings, Instruments More! * ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx ** * Cora Connection Your West African Music Source * * http://www.coraconnection.com/ * * Resources, Recordings, Instruments More! * ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: removing white space revisited
On Mon, 18 Feb 2002 at 18:48 GMT, David Gilden wrote: Hi, My goal is to clean up the fields submitted from a form and then be able to use them in other places. Do I have to assign my 'cleaned' data to a hash? You can try it by yourself: #!/usr/bin/perl -w use strict; use CGI qw(:cgi); foreach my $key ( param() ) { my $value = param($key);# Fetch the parameter value $value =~ s/^\s*|\d*\s*$//g;# Strip whitespaces and trailing digits param( $key, $value ); # Reassigning the value in the parameter } # We verify the parameters to see if the values were changed foreach ( param() ) { print $_ = ', param($_), '\n; } __END__ -- briac dynamic .sig on strike, we apologize for the inconvenience -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: removing white space revisited
David, cgi-lib.pl and CGI.pm do the same things, for all intents and purposes. I've not bothered to use CGI.pm, in part because of its overly cumbersome style and kludges. But that's my preference. There are very few 'shoulds' in perl and far fewer 'musts.' CGI.pm over cgi-lib.pl is neither of those. :o So, for giggles, here's the problem solved in CGI.pm #!/usr/bin/perl use CGI; $q = new CGI; $q-header; %params = $q-Vars; foreach $key (keys(%params)) { $params{$key} = s/^\s+//; $params{$key} =~ s/\s+$//; } # # whatever else # And, equally for giggles, here's the same thing in cgi-lib.pl: #!/usr/bin/perl require 'cgi-lib.pl'; ReadParse; print PrintHeader; foreach $key (keys(%in)) { $in{$key} = s/^\s+//; $in{$key} =~ s/\s+$//; } # # whatever else # One involves less typing. It's also clearer in the eye of the beholder. -jdm From: David Gilden [EMAIL PROTECTED] To: John M [EMAIL PROTECTED], [EMAIL PROTECTED] Subject: Re: removing white space revisited Date: Mon, 18 Feb 2002 14:13:34 -0500 That is kool, but I am using CGI.pm like we all should be :) On Monday, February 18, 2002 at 2:01 PM, [EMAIL PROTECTED] (John M) wrote: David, foreach $key (keys(%in)) { $in{$key} =~ s/^\s+//; $in{$key} =~ s/\s+$//; } That's all you have to do. -jdm (%in is generated under cgi-lib.pl) So back to my question do need to assign param() to hash? Thx, Dave From an earlier post: This is great for 'Upper-casing' just the first letter of a string! #!/usr/bin/perl -w my $key =today's Departure date; $key =~ s/([\w'-]+)/\u$1/g; print $key; From: David Gilden [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: removing white space revisited Date: Mon, 18 Feb 2002 13:48:46 -0500 Hi, My goal is to clean up the fields submitted from a form and then be able to use them in other places. Does the following modify the value of param('name') -- if I have a form element named 'name' -- permanently (like when you have an array) foreach my $key (param()){ $val = param($key), $value =~ s/^\s+//; $value =~ s/\s+$//; $key =~ s/\d$//; print $key: $val\n\n; } later in the script print param('email') # will this have been cleaned regrex in the up the loop above? Do I have to assign my 'cleaned' data to a hash? Thanks! Dave ** * Cora Connection Your West African Music Source * * http://www.coraconnection.com/ * * Resources, Recordings, Instruments More! * ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx ** * Cora Connection Your West African Music Source * * http://www.coraconnection.com/ * * Resources, Recordings, Instruments More! * ** _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: removing white space
The first is more efficient, the second is less typing. And actually the second one is incorrect, it should be this: Rob -Original Message- From: David Gilden [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 14, 2002 12:29 PM To: [EMAIL PROTECTED] Subject: removing white space Is this correct, I want to remove leading and trailing white space, Thanks Dave -- foreach $item (param()) { my $value = param($item); $value =~ s/^\s+//; $value =~ s/\s+$//; } better?? foreach $item (param()) { my $value = param($item); $value =~ s/^\s+(.*)\s+$/$1/; } - #!/usr/bin/perl use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use POSIX 'strftime'; $mailprog = '/usr/lib/sendmail'; # Retrieve Date $date = strftime('%A, %B %1d, %Y %I:%M %p',localtime) ,\n; # my %in = {}; # remove any extra spaces at the front or back foreach $item (param()) { my $value = param($item); $value =~ s/^\s+//; $value =~ s/\s+$//; } $email = param('email'); $realname = param('name'); ** * Cora Connection Your West African Music Source * * http://www.coraconnection.com/ * * Resources, Recordings, Instruments More! * ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: removing white space
Oops, I sent that before I was done (sliped with the fingers). The first is more efficient, the second is less typing. And actually the second one is incorrect, it should be this: $value =~ s/^\s*(.*)\s*$/$1/; You need to use \s* because one of them might match a space and the other might not. The way you had it would only with if there was both a leading and trailing whitespace char(s). But yes, either will work... although most will recommend the first option due to performance (although it is minimal unless the string is very large). Rob -Original Message- From: Hanson, Robert Sent: Thursday, February 14, 2002 12:36 PM To: 'David Gilden'; [EMAIL PROTECTED] Subject: RE: removing white space The first is more efficient, the second is less typing. And actually the second one is incorrect, it should be this: Rob -Original Message- From: David Gilden [mailto:[EMAIL PROTECTED]] Sent: Thursday, February 14, 2002 12:29 PM To: [EMAIL PROTECTED] Subject: removing white space Is this correct, I want to remove leading and trailing white space, Thanks Dave -- foreach $item (param()) { my $value = param($item); $value =~ s/^\s+//; $value =~ s/\s+$//; } better?? foreach $item (param()) { my $value = param($item); $value =~ s/^\s+(.*)\s+$/$1/; } - #!/usr/bin/perl use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use POSIX 'strftime'; $mailprog = '/usr/lib/sendmail'; # Retrieve Date $date = strftime('%A, %B %1d, %Y %I:%M %p',localtime) ,\n; # my %in = {}; # remove any extra spaces at the front or back foreach $item (param()) { my $value = param($item); $value =~ s/^\s+//; $value =~ s/\s+$//; } $email = param('email'); $realname = param('name'); ** * Cora Connection Your West African Music Source * * http://www.coraconnection.com/ * * Resources, Recordings, Instruments More! * ** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: removing white space
Has anyone used the whitespace module. I downloaded it from CPAN but I couldn't get it to work for me at all. YM
Re: removing white space
On Jun 26, David Gilden said: Is the following regrex the correct way to remove leading or trailing white space from a string? Use two passes. $data =~ s/^\s+)//; $data =~ s/\s+$)//; Err, extra )'s in there. $data =~ s/^\s+//; $data =~ s/\s+$//; -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun. Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/ Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/ Acacia Fraternity, Rensselaer Chapter. Brother #734 ** Manning Publications, Co, is publishing my Perl Regex book **
Re: removing white space
Hey david, this regexp will do the trick for you: $_ = '[EMAIL PROTECTED] (mike smith)'; s/^\s+|\s+$//g; you dont need the parenthesis around it, unless you want to capture the whitespace (which seems futile) i'd say compiling a regexp twice would be more of a drain on system resources then an | actually, but if you'd like you can use the benchmark module to time it. snip $data = [EMAIL PROTECTED] (mike smith) $data =~ s/(^\s+)|(\s+$)//g; or would it be more efficient to it thus: # two passes $data =~ s/^\s+)//; $data =~ s/\s+$)//; /snip if you're concerned with system performance, then you do NOT want to put an entire file into memory (especially using an array). it's concidered bad practice and there've beeen quite a few posts on this list about it. so here's what you should do in this case, seeing you're testing each element one by one anyway: while (EL) { next if /test/; s/^\s+|\s+$//g; other_stuff; } escaping the @ is not needed, perl will Do The Right Thing for you =) snip Final comment when I am reading from a loop: open(EL,$emailLog) || errorLog( Read in, Could not find $emailLog, $!); my @lines = EL; foreach(@lines){ next if /test/; $_ =~ s/^\s+(.*)\s+$/\1,/; other code Do I need to escape the '@' as my data will be of format: [EMAIL PROTECTED] (name) /snip hth, Jos Boumans
RE: removing white space
Is the following regrex the correct way to remove leading or trailing white space from a string? $data = [EMAIL PROTECTED] (mike smith) $data =~ s/(^\s+)|(\s+$)//g; This looks like it works to me. or would it be more efficient to it thus: # two passes $data =~ s/^\s+)//; $data =~ s/\s+$)//; I ran the following code: use Benchmark; use strict; use warnings; timethese (100, { pipe = q{ my $data = bla\@bla.net (mike smith); $data =~ s/(^\s+)|(\s+$)//g; } , separate = q{ my $data = bla\@bla.net (mike smith); $data =~ s/^\s+//; $data =~ s/\s+$//; } } ); And got this output... Benchmark: timing 100 iterations of pipe, separate... pipe: 19 wallclock secs (18.02 usr + 0.00 sys = 18.02 CPU) @ 55478.50/s (n=100) separate: 3 wallclock secs ( 3.72 usr + 0.00 sys = 3.72 CPU) @ 269106.57/s (n=100) So I think breaking it into 2 lines is much more efficient. Final comment when I am reading from a loop: open(EL,$emailLog) || errorLog( Read in, Could not find $emailLog, $!); my @lines = EL; foreach(@lines){ next if /test/; $_ =~ s/^\s+(.*)\s+$/\1,/; other code Do I need to escape the '@' as my data will be of format: [EMAIL PROTECTED] (name) No, maybe someone else can explain what's going on inside that avoids this, but I could swear I've done this sort of thing before and had no problems with my variable contents being interpolated as lists. BTW s/// operate on $_ by default, just like the m// in the 'next if' line. So you could just do s/^\s+(.*)\s+$/\1,/; on that line. Also $1 is preferred over \1 according to my 5.6.1 version. Peter C.