Counting text area

Discussion in 'Perl Misc' started by Davidd Sargent, Aug 13, 2004.

  1. 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
    Davidd Sargent, Aug 13, 2004
    #1
    1. Advertising

  2. Davidd Sargent

    Tore Aursand Guest

    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.


    --
    Tore Aursand <>
    "Poor management can increase software costs more rapidly than any
    other factor." (Barry Boehm)
    Tore Aursand, Aug 13, 2004
    #2
    1. Advertising

  3. Davidd Sargent wrote:

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

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Aug 13, 2004
    #3
  4. Davidd Sargent wrote:
    > 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
    Jürgen Exner, Aug 13, 2004
    #4
  5. 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

    "Jürgen Exner" <> a écrit dans le message de
    news:XG1Tc.17304$...
    > Davidd Sargent wrote:
    > > 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
    >
    >
    Davidd Sargent, Aug 13, 2004
    #5
  6. Davidd Sargent wrote:
    >> Davidd Sargent wrote:
    >>> 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)).

    >
    > $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};

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 13, 2004
    #6
  7. Gunnar Hjalmarsson wrote:
    > 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?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 13, 2004
    #7
  8. Davidd Sargent

    Joe Smith Guest

    Gunnar Hjalmarsson wrote:

    > 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
    Joe Smith, Aug 13, 2004
    #8
  9. 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

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 13, 2004
    #9
  10. "Gunnar Hjalmarsson" <> wrote in message
    news:...
    > 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

    >
    > --
    > Gunnar Hjalmarsson
    > Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Andrew Palmer, Aug 13, 2004
    #10
  11. 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
    Gunnar Hjalmarsson, Aug 13, 2004
    #11
  12. Davidd Sargent

    Anno Siegel Guest

    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
    Anno Siegel, Aug 14, 2004
    #12
  13. Davidd Sargent

    Anno Siegel Guest

    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
    Anno Siegel, Aug 14, 2004
    #13
  14. 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
    Gunnar Hjalmarsson, Aug 14, 2004
    #14
  15. Davidd Sargent

    Anno Siegel Guest

    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
    Anno Siegel, Aug 14, 2004
    #15
  16. 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
    Gunnar Hjalmarsson, Aug 14, 2004
    #16
  17. "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
    Andrew Palmer, Aug 14, 2004
    #17
  18. Davidd Sargent

    John S Guest

    Tore Aursand <> wrote in news:pan.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.
    John S, Aug 15, 2004
    #18
  19. 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
    Gunnar Hjalmarsson, Aug 15, 2004
    #19
  20. Davidd Sargent

    John S Guest

    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.
    John S, Aug 16, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    1
    Views:
    595
    Larry Barowski
    Jun 27, 2005
  2. hotadvice
    Replies:
    14
    Views:
    702
    hotadvice
    Oct 2, 2007
  3. Andy B.
    Replies:
    0
    Views:
    805
    Andy B.
    Apr 30, 2010
  4. leo
    Replies:
    1
    Views:
    276
    Bob Lehmann
    Dec 5, 2005
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    199
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page