Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to count delimiters?

Reply
Thread Tools

How to count delimiters?

 
 
A man
Guest
Posts: n/a
 
      01-06-2004
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.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      01-06-2004

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. |
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-06-2004
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.
>

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-06-2004
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
 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      01-06-2004
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

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-07-2004
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


 
Reply With Quote
 
A man
Guest
Posts: n/a
 
      01-07-2004
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.
 
Reply With Quote
 
A man
Guest
Posts: n/a
 
      01-07-2004
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.
 
Reply With Quote
 
A man
Guest
Posts: n/a
 
      01-07-2004
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.
 
Reply With Quote
 
A man
Guest
Posts: n/a
 
      01-07-2004

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.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Count = Count + 1 Using only std_logic_1164 Doubt efelnavarro09 VHDL 2 01-26-2011 03:49 AM
TBird: Unread NewsGroup Message Count Never Accurate Herb Firefox 4 03-29-2005 02:00 AM
Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from? Kaimuri MCSD 3 12-29-2004 06:38 PM
I am adding a new row to the datagrid dynamically but if i use the Count property of Item it is not showing the count of the new rows being added Praveen Balanagendra via .NET 247 ASP .Net 2 06-06-2004 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57