Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Counting text area

Reply
Thread Tools

Counting text area

 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-13-2004
Andrew Palmer wrote:
> Gunnar Hjalmarsson wrote:
>> Joe Smith wrote:
>>> Gunnar Hjalmarsson wrote:
>>>>
>>>> my $count = 0;
>>>> $count ++ for split /\n/, $in{packageID};
>>>
>>> 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";

>
> my $count = split /\n/, $in{packageID};
> print "$count\n";
>
>> Outputs:
>> 1
>> 3

>
> 3


Hey, did you read the docs or something?

Thanks!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-14-2004
Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
> Joe Smith wrote:
> > Gunnar Hjalmarsson wrote:
> >>
> >> my $count = 0;
> >> $count ++ for split /\n/, $in{packageID};

> >
> > 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


That can be fixed:

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

Anno
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      08-14-2004
Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
> Andrew Palmer wrote:
> > Gunnar Hjalmarsson wrote:
> >> Joe Smith wrote:
> >>> Gunnar Hjalmarsson wrote:
> >>>>
> >>>> my $count = 0;
> >>>> $count ++ for split /\n/, $in{packageID};
> >>>
> >>> 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";

> >
> > my $count = split /\n/, $in{packageID};
> > print "$count\n";
> >
> >> Outputs:
> >> 1
> >> 3

> >
> > 3

>
> 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

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-14-2004
Anno Siegel wrote:
> Gunnar Hjalmarsson wrote:
>> Joe Smith wrote:
>>> Gunnar Hjalmarsson wrote:
>>>>
>>>> my $count = 0;
>>>> $count ++ for split /\n/, $in{packageID};
>>>
>>> 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

>
> 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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-14-2004
Gunnar Hjalmarsson <> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> > Gunnar Hjalmarsson wrote:
> >> Joe Smith wrote:
> >>> Gunnar Hjalmarsson wrote:
> >>>>
> >>>> my $count = 0;
> >>>> $count ++ for split /\n/, $in{packageID};
> >>>
> >>> 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

> >
> > 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?


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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-14-2004
Anno Siegel wrote:
> Gunnar Hjalmarsson wrote:
>> Anno Siegel wrote:
>>>
>>> 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?

>
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Andrew Palmer
Guest
Posts: n/a
 
      08-14-2004

"Gunnar Hjalmarsson" <> wrote in message
news:...
> Andrew Palmer wrote:
> > Gunnar Hjalmarsson wrote:
> >> Joe Smith wrote:
> >>> Gunnar Hjalmarsson wrote:
> >>>>
> >>>> my $count = 0;
> >>>> $count ++ for split /\n/, $in{packageID};
> >>>
> >>> 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";

> >
> > my $count = split /\n/, $in{packageID};
> > print "$count\n";
> >
> >> Outputs:
> >> 1
> >> 3

> >
> > 3

>
> 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"


>
> Thanks!
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl




 
Reply With Quote
 
John S
Guest
Posts: n/a
 
      08-15-2004
Tore Aursand <> wrote in newsan.2004.08.13.08.48.02.412737
@aursand.no:

> On Fri, 13 Aug 2004 10:26:56 +0200, Davidd Sargent wrote:
>> 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.
>
>


textarea comes back with \r as well.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-15-2004
John S wrote:
> Tore Aursand wrote:
>> On Fri, 13 Aug 2004 10:26:56 +0200, Davidd Sargent wrote:
>>
>>> 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.

>
> 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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
John S
Guest
Posts: n/a
 
      08-16-2004
Gunnar Hjalmarsson <> wrote in news:2o9scnF8db61U1@uni-
berlin.de:

> John S wrote:
>> Tore Aursand wrote:
>>> On Fri, 13 Aug 2004 10:26:56 +0200, Davidd Sargent wrote:
>>>
>>>> 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.

>>
>> 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.
>


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MVC2: linking from one area view to another area view Andy B. ASP .Net 0 04-30-2010 07:46 AM
do structure definitions go in data area or in code area... hotadvice C Programming 14 10-02-2007 04:10 AM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Add point to graphics area without having to paint the whole area? Mark Java 1 06-27-2005 03:09 AM



Advertisments