Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Extracting the number of %'s in a string

by mnranjeeth (Novice)
on Dec 13, 2007 at 07:35 UTC ( #656765=perlquestion: print w/ replies, xml ) 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
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 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 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 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://656765]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-10-23 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (129 votes), past polls