Solved

Extracting Whitespaces HTML/CGI (really easy)

Posted on 2003-11-30
13
327 Views
Last Modified: 2010-03-04
(this is more of an html/cgi problem i guess)

How do i extract whitespaces from a TEXTAREA and count how many characters there are?



Thanks,
Ben
0
Comment
Question by:NeoHacker
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 9845412
Please explain some more about what you are asking.

When your CGI script receives the request from the user's browser, the form variables, including the TEXTAREA will be accessible (if you are using the CGI module, as you probably should) as

$txt = param('name_of_text_area');

The character count you are after may be as simple as "length($txt)". As for extracting whitespaces, you need to explain: are you asking how to remove _extra_ whitespace from the TEXTAREA, such as trimming extra spaces from the front and end? Or double spaces between words? Or what?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9846204
use CGI;
my $q = new CGI;
$txt=param('name_of_text_area');
$txt=~s/\s//g;@n=split(//,$txt)
print scalar @n;
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9846297
Ahoffman,

Okay, that's an interpretation: how many non-whitespace characters are there in the TEXTAREA? But why go through all those gyrations to get something that's equivalent to

print length($txt);

(I wonder if that needs a \n on the end?).
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 51

Expert Comment

by:ahoffmann
ID: 9846338
hmm, probably folloing what is asked for:
  $txt=~s/[^\s]//g;@n=split(//,$txt);

depending on interpretation of "extract whitespaces" ;-)
0
 

Author Comment

by:NeoHacker
ID: 9894107
sorry, i didnt mean EXTRACT them, i meant keep the whitespaces but COUNT the characters WITHOUT INCLUDING the whitespaces.
- i hope that clears thing up
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9894145
Okay, Ahoffman had it right.

One recommended way to count characters is to use the 'tr' operator.

$count = $string =~ tr/ \t\r\n//c;

This does not actually change $string, but it counts all characters in the complement of the set specified as space, tab, carriage return, and line feed.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 70 total points
ID: 9895205
($x=$txt)=~s/\s//g;
print scalar split(//,$x);
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9898361
Ahoffman, I don't want to criticize, but why do you take the extra (and expensive) step of splitting a string into an array of single characters just to count them? The number of characters in the string will always be more cheaply obtained using 'length'. Am I missing something?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9898928
> .. don't want to criticize
don't worry, I'm learning too, sometimes, somehow ;-)
The extra step? you mean split? then you're correct.
But how about more perlish:
  $_=$txt; print tr/ \t\r\n//c;
0
 
LVL 84

Expert Comment

by:ozo
ID: 9900627
Ot if you don't want $_ to trash $_
  print tr/ \t\r\n//c for $txt;
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9909991
Neohacker,

Hasn't your question been answered? I recommend that you accept one of Ahoffman's responses and close this question. Otherwise, if there's still something we can do for you, please ask.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9910976
BTW, when using split() in a scalar context, it does not generate an array
0
 

Author Comment

by:NeoHacker
ID: 9917291
Ok, thanks ahoffman. Since you've helped me a lot on my questions, shouldn't i raise 20 points?

thanks,
ben
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question