Your skill will accomplishwhat the force of many cannot PerlMonks

### Extracting the number of %'s in a string

by mnranjeeth (Novice)
 on Dec 13, 2007 at 07:35 UTC Need Help??
mnranjeeth has asked for the wisdom of the Perl Monks concerning the following question:

I have a string in the following format:
"-----%s----%a----%d--%s-%d---" ('-' can be anything).

I need to find out the
1. number of occurences %x (x is the character following the %) in the string
2. and the order in which they appear.

Is it possible to do it using a regex or is converting the string into an array and then processing it is the only way out?
If so what would be the most efficient way to do it?

• Comment on Extracting the number of %'s in a string

Replies are listed 'Best First'.
Re: Extracting the number of %'s in a string
by ikegami (Pope) on Dec 13, 2007 at 07:44 UTC

Should "%%a" count as one (x=%) or two (x=%,x=a)? The solution below returns the former.

```my \$count = my @order = \$string =~ /%(.)/sg;

Or if you want the count for each different "x" instead of one global count:

```\$count{\$_}++ for my @order = \$string =~ /%(.)/sg;

Update: Added second solution.

Re: Extracting the number of %'s in a string
by moritz (Cardinal) on Dec 13, 2007 at 07:48 UTC
```use strict;
use warnings;
use Data::Dumper;

my @list = ("-----%s----%a----%d--%s-%d---" =~ m/\%./g);
print Dumper \@list;
__END__
\$VAR1 = [
'%s',
'%a',
'%d',
'%s',
'%d'
];
Re: Extracting the number of %'s in a string
by pc88mxer (Vicar) on Dec 13, 2007 at 07:45 UTC
```while (\$x =~ m/%(.)/g) {
push(@order, \$1);
\$count{\$1}++;
}
At the end of the while loop, @order contains the order in which the %-codes were found, and %count contains the counts.
Re: Extracting the number of %'s in a string
by poolpi (Hermit) on Dec 13, 2007 at 07:59 UTC
```my \$s = "-----%s----%a----%d--%s-%d---";

print \$s =~ tr/%//, "\n";

print "\$_\n" for ( \$s =~ /%([ads])/g );

OUTPUT :
5
s
a
d
s
d
HTH,
PooLpi
Re: Extracting the number of %'s in a string
by Zaxo (Archbishop) on Dec 13, 2007 at 09:46 UTC

To count them,

```\$_=q(aaa%bbbbb%cccccc%ddd);
print tr/%//, \$/;
prints '3'

To extract their positions;

```\$_ = 'aaa%bbbbb%cccccc%ddd';
print \$-[0], ' ' while /%/g;
print \$/'
prints "3 9 16".

See perlvar for the @- array magic. You would push those values to an array, instead of printing, for further use.

After Compline,
Zaxo

The counting solution will give a wrong result if the string has a trailing %, because the OP wants the number of %s followed by another character.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://656765]
Approved by ikegami
help
Chatterbox?
 [davido]: I have nvidia (1050ti) in one system at home. My work laptop came with a radeon/amd video system that is no longer supported by debian/ubuntu. So that being the case I'm in the "prefer nvidia even if proprietary" camp [davido]: Especially since the laptop is only a couple years old.

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2017-05-26 14:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (190 votes). Check out past polls.