How to count delimiters?

Discussion in 'Perl Misc' started by A man, Jan 6, 2004.

  1. A man

    A man Guest

    How do I count the number of delimiters in a string when split drops
    any null fields at the end of a string? Example:
    My string=1,2,3,,4,5,,,,

    I want to count how many delimiters there are, which should be 8. But
    if I do a split I get:
    1
    2
    3
    (null)
    4
    5

    I need to count those other commas at the end. Does anyone know how
    to do this?

    I tried $n=$l=~m/,/ but that only returns 1 if the string exists in
    $l.

    --
    Say no to fixed width tables. They look terrible in all browsers.
    A man, Jan 6, 2004
    #1
    1. Advertising

  2. A man

    Ben Morrow Guest

    A man <> wrote:
    > How do I count the number of delimiters in a string when split drops
    > any null fields at the end of a string? Example:
    > My string=1,2,3,,4,5,,,,
    >
    > I want to count how many delimiters there are, which should be 8.


    I make it 9 delimiters, or 10 fields.

    > But if I do a split I get:

    <snip>
    > I need to count those other commas at the end. Does anyone know how
    > to do this?


    my $string = "1,2,3,,4,5,,,,";

    print $string =~ tr/,//;
    # prints 9, the number of delimiters

    print scalar grep {1} split /,/, $string, -1;
    # prints 10, the number of fields
    # the reason for the grep {1} is to avoid calling split in scalar
    # context which would clobber @_

    Ben

    --
    Musica Dei donum optimi, trahit homines, trahit deos. |
    Musica truces mollit animos, tristesque mentes erigit. |
    Musica vel ipsas arbores et horridas movet feras. |
    Ben Morrow, Jan 6, 2004
    #2
    1. Advertising

  3. A man

    Paul Lalli Guest

    I don't know that it's the best way, but to fix your attempt:

    ++$n while $string =~ /,/g;

    The g modifier searches out each instance of the pattern within the
    string, and saves the point of the last match to be used the next time the
    pattern match is executed.

    Paul Lalli

    On Tue, 6 Jan 2004, A man wrote:

    > Date: Tue, 6 Jan 2004 16:12:31 -0500
    > From: A man <>
    > Newsgroups: comp.lang.perl.misc
    > Subject: How to count delimiters?
    >
    > How do I count the number of delimiters in a string when split drops
    > any null fields at the end of a string? Example:
    > My string=1,2,3,,4,5,,,,
    >
    > I want to count how many delimiters there are, which should be 8. But
    > if I do a split I get:
    > 1
    > 2
    > 3
    > (null)
    > 4
    > 5
    >
    > I need to count those other commas at the end. Does anyone know how
    > to do this?
    >
    > I tried $n=$l=~m/,/ but that only returns 1 if the string exists in
    > $l.
    >
    > --
    > Say no to fixed width tables. They look terrible in all browsers.
    >
    Paul Lalli, Jan 6, 2004
    #3
  4. A man <> wrote:

    > How do I count the number of delimiters in a string
    > any null fields at the end of a string? Example:
    > My string=1,2,3,,4,5,,,,
    >
    > I want to count how many delimiters there are, which should be 8.



    There are zero delimiters, 9 separators and 10 fields in that data.


    > Does anyone know how
    > to do this?


    my $seps = $str =~ tr/,//;

    If you want to know how many fields:

    my $fields = $str =~ tr/,// + 1;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 6, 2004
    #4
  5. A man <> wrote:
    > How do I count the number of delimiters in a string when split drops
    > any null fields at the end of a string? Example:


    As Ben Morrow noted, you can tell split to keep null fields:

    split /PATTERN/,EXPR,LIMIT
    ...
    If LIMIT is unspecified or zero, trailing
    null fields are stripped (which potential users of
    "pop" would do well to remember). If LIMIT is
    negative, it is treated as if an arbitrarily large
    LIMIT had been specified.

    So,
    $string = '1,2,3,,4,5,,,,';
    @elements = split /,/, $string, -1;
    print "$#elements delimiters\n";

    --
    Glenn Jackman
    NCF Sysadmin
    Glenn Jackman, Jan 6, 2004
    #5
  6. A man wrote:
    > How do I count the number of delimiters in a string when split drops
    > any null fields at the end of a string? Example:
    > My string=1,2,3,,4,5,,,,
    >
    > I want to count how many delimiters there are, which should be 8.


    8? Which character do you consider to be the delimiter? It's not the comma,
    isn't it?

    > But if I do a split I get:


    Well, split is rarely the right tool to count characters.
    Why not use simple
    $count = $string =~ tr/,//;
    But that returns a count of 9, not of 8 as requested.

    jue
    Jürgen Exner, Jan 7, 2004
    #6
  7. A man

    A man Guest

    In article <btf94g$95$>,
    spoke thusly...
    >
    > A man <> wrote:
    > > How do I count the number of delimiters in a string when split drops
    > > any null fields at the end of a string? Example:
    > > My string=1,2,3,,4,5,,,,
    > >
    > > I want to count how many delimiters there are, which should be 8.

    >
    > I make it 9 delimiters, or 10 fields.


    You're right, 9 delimiters. Thanks for the info.

    --
    Say no to fixed width tables. They look terrible in all browsers.
    A man, Jan 7, 2004
    #7
  8. A man

    A man Guest

    In article <xAIKb.2226$>,
    spoke thusly...
    > A man wrote:
    > > How do I count the number of delimiters in a string when split drops
    > > any null fields at the end of a string? Example:
    > > My string=1,2,3,,4,5,,,,
    > >
    > > I want to count how many delimiters there are, which should be 8.

    >
    > 8? Which character do you consider to be the delimiter? It's not the comma,
    > isn't it?


    I was wrong. There are 9 delimiters (commas).
    But thanks.

    --
    Say no to fixed width tables. They look terrible in all browsers.
    A man, Jan 7, 2004
    #8
  9. A man

    A man Guest

    In article <btf94g$95$>,
    spoke thusly...
    >
    > A man <> wrote:
    > > How do I count the number of delimiters in a string when split drops
    > > any null fields at the end of a string? Example:
    > > My string=1,2,3,,4,5,,,,
    > >
    > > I want to count how many delimiters there are, which should be 8.

    >
    > I make it 9 delimiters, or 10 fields.
    >
    > > But if I do a split I get:

    > <snip>
    > > I need to count those other commas at the end. Does anyone know how
    > > to do this?

    >
    > my $string = "1,2,3,,4,5,,,,";
    >
    > print $string =~ tr/,//;
    > # prints 9, the number of delimiters
    >
    > print scalar grep {1} split /,/, $string, -1;
    > # prints 10, the number of fields
    > # the reason for the grep {1} is to avoid calling split in scalar
    > # context which would clobber @_


    I thought that would work if my delimiter was <Tc> but it doesn't. So
    how would I count the number of delimiters if the delim was <Tc> and
    the string was below?
    $s="1<Tc>2<Tc>3<Tc><Tc>5<Tc><Tc><Tc>";

    --
    Say no to fixed width tables. They look terrible in all browsers.
    A man, Jan 7, 2004
    #9
  10. A man

    A man Guest

    I got it. Use 's' instead of 'tr'.
    $string="1<Tc>2<Tc>3<Tc><Tc>5<Tc><Tc><Tc>";

    print $string =~ s/<Tc>//;

    This prints "7".

    --
    Say no to fixed width tables. They look terrible in all browsers.
    A man, Jan 7, 2004
    #10
  11. A man <> wrote:
    > In article <btf94g$95$>,
    > spoke thusly...
    >> A man <> wrote:


    >> > How do I count the number of delimiters in a string



    But now it looks like that is not what you want to count.

    Now you want to count the fields instead. Right?


    >> print scalar grep {1} split /,/, $string, -1;
    >> # prints 10, the number of fields


    > I thought that would work if my delimiter was <Tc>



    It will.


    > but it doesn't.



    Then you are in your own little Twilight Zone. We won't be able to help.


    > So
    > how would I count the number of delimiters if the delim was <Tc> and
    > the string was below?
    > $s="1<Tc>2<Tc>3<Tc><Tc>5<Tc><Tc><Tc>";



    Have you seen the Posting Guidelines that are posted here frequently?

    A short and complete program shows that it works just fine if you
    change the separator.

    -----------------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    my $string = '1<Tc>2<Tc>3<Tc><Tc>5<Tc><Tc><Tc>';
    print scalar grep {1} split /<Tc>/, $string, -1;
    -----------------------------------

    prints: 8


    Works for me...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 7, 2004
    #11
  12. A man <> wrote:

    > $string="1<Tc>2<Tc>3<Tc><Tc>5<Tc><Tc><Tc>";
    >
    > print $string =~ s/<Tc>//;
    >
    > This prints "7".



    No it doesn't.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 8, 2004
    #12
  13. A man

    Tore Aursand Guest

    On Wed, 07 Jan 2004 13:28:32 -0500, A man wrote:
    > I got it. Use 's' instead of 'tr'.
    > $string="1<Tc>2<Tc>3<Tc><Tc>5<Tc><Tc><Tc>";
    >
    > print $string =~ s/<Tc>//;
    >
    > This prints "7".


    No, it doesn't. If so, something is _very_ wrong. Did you try the code
    above?

    Anyway. In seems to me that you have 8 fields in $string. I would have
    tried this to get the values;

    my $string = '1<Tc>2<Tc>3<Tc><Tc>5<Tc><Tc><Tc>';
    my @values = split( '<Tc>', $string, -1 );


    --
    Tore Aursand <>
    "Time only seems to matter when it's running out." -- Peter Strup
    Tore Aursand, Jan 8, 2004
    #13
  14. A man

    Andy Baxter Guest

    At earth time Tue, 06 Jan 2004 16:12:31 -0500, the following transmission
    was received from the entity known as A man:

    > How do I count the number of delimiters in a string when split drops
    > any null fields at the end of a string? Example:
    > My string=1,2,3,,4,5,,,,
    >
    > I want to count how many delimiters there are, which should be 8. But
    > if I do a split I get:
    > 1
    > 2
    > 3
    > (null)
    > 4
    > 5
    >
    > I need to count those other commas at the end. Does anyone know how
    > to do this?
    >
    > I tried $n=$l=~m/,/ but that only returns 1 if the string exists in
    > $l.


    In the spirit of 'there is more than one way to do it', how about:

    bash-2.05b$ perl
    my $string='1,2,3,,4,5,,,,';
    @fields=split /,/, $string.'notacomma';
    foreach $field (@fields) {
    print "$field\n";
    };
    <ctrl-D>
    1
    2
    3

    4
    5



    notacomma

    not the best way though.

    --
    http://www.niftybits.ukfsn.org/

    remove 'n-u-l-l' to email me. html mail or attachments will go in the spam
    bin unless notified with
    HTML:
     or [attachment] in the subject line.
    Andy Baxter, Jan 8, 2004
    #14
  15. A man

    Anno Siegel Guest

    Jürgen Exner <> wrote in comp.lang.perl.misc:
    > A man wrote:
    > > How do I count the number of delimiters in a string when split drops
    > > any null fields at the end of a string? Example:
    > > My string=1,2,3,,4,5,,,,
    > >
    > > I want to count how many delimiters there are, which should be 8.

    >
    > 8? Which character do you consider to be the delimiter? It's not the comma,
    > isn't it?
    >
    > > But if I do a split I get:

    >
    > Well, split is rarely the right tool to count characters.
    > Why not use simple
    > $count = $string =~ tr/,//;
    > But that returns a count of 9, not of 8 as requested.


    That method doesn't apply to delimiters of more than one character,
    like "<Tc>", which the OP mentioned somewhere.

    The canonical method of match counting doesn't use split(), as mostly
    suggested in this thread. It's the slightly obscure

    my $count = () = /<Tc>/g;

    It is based on the queer but useful fact that a list assignment, used
    in scalar context, returns the original number of elements, not the
    number of elements that were in fact assigned.

    Anno
    Anno Siegel, Jan 8, 2004
    #15
  16. Anno Siegel wrote:
    > Jürgen Exner <> wrote in comp.lang.perl.misc:
    > That method doesn't apply to delimiters of more than one character,
    > like "<Tc>", which the OP mentioned somewhere.


    Yeah, right.
    The OP changed the specification of his problem at some point during the
    thread.

    jue
    Jürgen Exner, Jan 9, 2004
    #16
    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. brrrdog
    Replies:
    0
    Views:
    828
    brrrdog
    Jul 9, 2003
  2. ATC Productions
    Replies:
    3
    Views:
    2,583
    ATC Productions
    May 28, 2005
  3. flarosa
    Replies:
    6
    Views:
    11,953
    flarosa
    Apr 11, 2006
  4. Todd Beauchemin

    overloaded >> and istream delimiters

    Todd Beauchemin, Jul 31, 2003, in forum: C++
    Replies:
    1
    Views:
    445
    Victor Bazarov
    Jul 31, 2003
  5. shea martin
    Replies:
    4
    Views:
    384
    Bob Hairgrove
    Aug 30, 2004
Loading...

Share This Page