How to count delimiters?

A

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

Ben Morrow

A man said:
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:
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
 
P

Paul Lalli

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
 
T

Tad McClellan

A man said:
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;
 
G

Glenn Jackman

A man said:
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";
 
J

Jürgen Exner

A said:
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
 
A

A man

I make it 9 delimiters, or 10 fields.



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

A man

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

Tad McClellan

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

Now you want to count the fields instead. Right?

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

Tore Aursand

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 );
 
A

Andy Baxter

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

Anno Siegel

Jürgen Exner said:
8? Which character do you consider to be the delimiter? It's not the comma,
isn't it?


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
 
J

Jürgen Exner

Anno said:
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top