Counting text area

D

Davidd Sargent

Hi Everybody,
I'm new to this group.
I am trying to make a script but have a slight problem so I turned to you
guys.

I have a textarea which the user types there subdomain in to and then click
submit.
What I want is on the results page for it to count the number of subdomains
(each domain will be seperated by \n (a newline)).

Thanks in Advance

Kind Regards


Dave
 
T

Tore Aursand

What I want is on the results page for it to count the number of
subdomains (each domain will be seperated by \n (a newline)).

1. Read the FORM data by using CGI.pm.
2. Get the lines by splitting on "\n"
3. Process each line, discard those which doesn't meet your "subdomain
requirements".
4. Present the result for the user.
 
S

Sherm Pendley

Davidd said:
I have a textarea which the user types there subdomain in to and then click
submit.
What I want is on the results page

Textarea? Submit? Results page? That doesn't sound like Perl to me. (I'm
not just saying that to be snippy - precisely defining a problem is the
first step towards solving it.)
for it to count the number of subdomains
(each domain will be seperated by \n (a newline)).

So your Perl problem is, you have a block of text in a scalar, and you
want to count the lines in it.

Use split() to split the text into an array of lines. Then use that
array in scalar context to count the number of elements in it.

sherm--
 
J

Jürgen Exner

Davidd said:
I have a textarea which the user types there subdomain in to and then
click submit.
What I want is on the results page for it to count the number of
subdomains (each domain will be seperated by \n (a newline)).

Perl doesn't have text areas, submit clicks, or pages.
Trying to rephrase your question to make sure I guessed right on what you
were looking for:

Given a scalar containing muli-line text (separated by standard \n) how can
I determine how many lines of text the scalar contains?

use strict; use warnings;
my $foo = "asdf\nlkjh\nfoo\nbar\n\n";
my $newlines = () = $foo =~/\n/g;
print $newlines;

Further details see "perldoc perlop", section "Regexp Quote-Like Operators",
in particular the "g" modifier.

jue
 
D

Davidd Sargent

My Code
Sorry I'm a beginner.

$tempvar = $in{'packageID'};
$line = "\n";
@newtextareaarray = split($tempvar,$line);
$count ="0";
foreach $line (@newtextareaarray) {
$count++;

}


print qq|Here : $count|;


Thanks

Dave
 
G

Gunnar Hjalmarsson

Davidd said:
$tempvar = $in{'packageID'};
$line = "\n";
@newtextareaarray = split($tempvar,$line);
$count ="0";
foreach $line (@newtextareaarray) {
$count++;

}

You should look up and read about the split() function in the docs:

perldoc -f split

Also, you don't need all those temporary variables. This should do
what you want:

my $count = 0;
$count ++ for split /\n/, $in{packageID};
 
G

Gunnar Hjalmarsson

Gunnar said:
This should do what you want:

my $count = 0;
$count ++ for split /\n/, $in{packageID};

I.e. as long as you trust the users to only type subdomains, and to
put one subdomain per line...

Have you thought about how to tell your program how it should
recognize a subdomain?
 
J

Joe Smith

Gunnar said:
Also, you don't need all those temporary variables. This should do
what you want:

my $count = 0;
$count ++ for split /\n/, $in{packageID};

my $count = () = split /\n/,$in{packageID};

-Joe
 
G

Gunnar Hjalmarsson

Joe said:
my $count = () = split /\n/,$in{packageID};

my %in = ( packageID => "one\ntwo\nthree" );

my $count = () = split /\n/, $in{packageID};
print "$count\n";

$count = 0;
$count ++ for split /\n/, $in{packageID};
print "$count\n";

Outputs:
1
3
 
A

Andrew Palmer

Gunnar Hjalmarsson said:
my %in = ( packageID => "one\ntwo\nthree" );

my $count = () = split /\n/, $in{packageID};
print "$count\n";

$count = 0;
$count ++ for split /\n/, $in{packageID};
print "$count\n";

my $count = split /\n/, $in{packageID};
print "$count\n";
 
A

Anno Siegel

Gunnar Hjalmarsson said:
my %in = ( packageID => "one\ntwo\nthree" );

my $count = () = split /\n/, $in{packageID};
print "$count\n";

$count = 0;
$count ++ for split /\n/, $in{packageID};
print "$count\n";

Outputs:
1
3

That can be fixed:

my $count = () = split /\n/,$in{packageID}, -1;

Anno
 
A

Anno Siegel

Gunnar Hjalmarsson said:
Hey, did you read the docs or something?

Thanks! ;-)

Have you run it under warnings? "Use of implicit split to @_ is
deprecated...".

This warning can not be suppressed by "no warnings 'deprecated'", nor
by any other warnings category I've tried. So this would end up as
something like

my $count = do { no warnings; split /\n/, $in{packageID} };

which is not very nice.

Facit: split() is far too clever for its own good. Ilya has recently
pointed this out too.

Anno
 
G

Gunnar Hjalmarsson

Anno said:
That can be fixed:

my $count = () = split /\n/,$in{packageID}, -1;

Is it possible to figure out from the docs that LIMIT makes a
difference in that respect?

Wonder why I suggested the

$count ++ for split /\n/, $in{packageID};

solution? ;-)
 
A

Anno Siegel

Gunnar Hjalmarsson said:
Is it possible to figure out from the docs that LIMIT makes a
difference in that respect?

Indirectly. The reason that we see $count = 1 is that split in list
context figures out how many elements are expected and, if there is
a bound, assumes a LIMIT of one more than that. (One more so that
the remainder of the string doesn't cling to the last element.)
Supplying an explicit, but ineffective, LIMIT of -1 stops that from
happening.

Actually, a LIMIT of 0 would be better because it behaves like no LIMIT
with respect to trailing empty fields. If the string is closed with a
line feed, as when it is read from a file, that makes a difference.

Did I mention split() is too clever for its own good?
Wonder why I suggested the

$count ++ for split /\n/, $in{packageID};

solution? ;-)

Not at all.

my $count = @{ [ split /\n/,$in{packageID}]};

could also be used.

Anno
 
G

Gunnar Hjalmarsson

Anno said:
Indirectly. The reason that we see $count = 1 is that split in
list context figures out how many elements are expected and, if
there is a bound, assumes a LIMIT of one more than that. (One more
so that the remainder of the string doesn't cling to the last
element.) Supplying an explicit, but ineffective, LIMIT of -1 stops
that from happening.

Actually, a LIMIT of 0 would be better because it behaves like no
LIMIT with respect to trailing empty fields. If the string is
closed with a line feed, as when it is read from a file, that makes
a difference.

Aha, thanks for the explanation.
Did I mention split() is too clever for its own good?

Yes, you did, and unless you consider 'clever' eq 'complex', I
disagree. Such design is not cleverness to me. ;-)
 
A

Andrew Palmer

Gunnar Hjalmarsson said:
Hey, did you read the docs or something?

I wish I could claim such a high virtue, but obviously not!

From the second paragraph: "Use of split in scalar context is deprecated"
 
J

John S

1. Read the FORM data by using CGI.pm.
2. Get the lines by splitting on "\n"
3. Process each line, discard those which doesn't meet your "subdomain
requirements".
4. Present the result for the user.

textarea comes back with \r as well.
 
G

Gunnar Hjalmarsson

John said:
textarea comes back with \r as well.

Only on Windows (of course), and not if you use a library that takes
care of it, such as CGI.pm (as Tore suggested) or cgi-lib.pl.
Otherwise you can binmode STDOUT to prevent the \r pecularity.
 
J

John S

Only on Windows (of course), and not if you use a library that takes
care of it, such as CGI.pm (as Tore suggested) or cgi-lib.pl.
Otherwise you can binmode STDOUT to prevent the \r pecularity.

When this is run from an Apache server:
----------------------------------------------------
#!/usr/bin/perl -T

use strict;
use warnings;
use CGI;

my $q = new CGI;

my $fe = $q->param( 'friend_email' ); # text area

my $msg;
if ( $fe =~ /\r/ ){
$msg = 'found \r'
}
else{
$msg = '\r not found'
}

print $q -> header,
$q -> start_html( -title=>'test' ),
$q -> p( 'testing text area ),
$q -> p( $fe ),
$q -> p( $msg ),
$q -> end_html;
-----------------------------------------------------------

I get 'found \r'.
Normally it doesn't matter until, as in this case, it is a list of emails
for example. It took me ages to track it down.
Used IE 6 browser on XP.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top