Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

string manipulation

by kimlid2810 (Acolyte)
on May 18, 2013 at 19:03 UTC ( #1034131=perlquestion: print w/ replies, xml ) Need Help??
kimlid2810 has asked for the wisdom of the Perl Monks concerning the following question:

hi monks. so i have a simple i think question, but i cant find any easy or fast solution, so it is not so simple to me. :p Say, there is this scenario, where you have a variable which contains a string full of ones and zeroes like:

$string = "010011100001110110100110111000001";

What do you think would be the quickest, not only for the cpu but for writing too, way to create another string, with the number of continual zeroes in it. Let's say for the above $string, to create a new one with this format:

$string = "010011100001110110100110111000001"; $newString = "1, 2, 4, 1, 1, 2, 1, 5";

where 1 is for the first zero in $string, 2 for the 2 zeroes in third and fourth place, 4 for the 4 zeroes in 8th, 9th, 10th, and 11th place. etc... I m really sorry if you cant really make out what i mean, but english is not my native language. Any help would be appreciated.

Comment on string manipulation
Select or Download Code
Re: string manipulation
by hdb (Parson) on May 18, 2013 at 19:15 UTC

    Splitting on 1's, then counting with length.

    use strict; use warnings; my $string = "010011100001110110100110111000001"; my $newstring = join ",", map { length $_ } split /1+/, $string; print "$newstring\n";
Re: string manipulation
by aitap (Deacon) on May 18, 2013 at 19:16 UTC

    my $answer = join ", ", map { length $_ } $string =~ /(0+)/g;

    1. $string =~ /(0+)/g searches for occurances of 0's in your string and returns an array of them
    2. map { length $_ } ... transforms the array of strings into an array of their lengths (i.e. counts of zeroes)
    3. Finally, join ", " ... makes a string from this array.
    More info: perlretut, map, join.

    Edit: and I was a bit late :)

Re: string manipulation
by choroba (Abbot) on May 18, 2013 at 19:18 UTC
    I am not sure this is fast to write (it was for me, though). We will have to wait for other solutions to benchmark its speed.
    my $newString = join ', ', map length, split /1+/, $string;
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: string manipulation
by Not_a_Number (Parson) on May 18, 2013 at 19:19 UTC
    print join ', ', map length, split /1+/, $string;

    Update: Beaten to it by hdb and choroba. Should have re-checked existing replies before posting. Mea culpa.

Re: string manipulation
by kimlid2810 (Acolyte) on May 18, 2013 at 19:24 UTC
    ok guys. thank you all for your replies and you time. i guess 4 in 4 answers being the same, that must really mean something. ;) thanks again :)
      Take a look at pack it has a count pattern feature. (already discussed here, see archives)

      Though not sure if it meets your requirements.

      If it does it will for sure be the fastest approach.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re: string manipulation
by roboticus (Canon) on May 18, 2013 at 21:17 UTC

    kimlid2810:

    Just for something a little different:

    $ cat t.pl my $string = "010011100001110110100110111000001"; $string =~ s/1+/, /g; $string =~ s/(0+)/length($1)/ge; print $string; $ perl t.pl 1, 2, 4, 1, 1, 2, 1, 5,

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: string manipulation
by johngg (Abbot) on May 18, 2013 at 23:57 UTC

    A slight variation on the split and count solutions.

    $ perl -E ' > $str = q{010011100001110110100110111000001}; > say join q{,}, map tr{0}{}, split m{1+}, $str;' 1,2,4,1,1,2,1,5 $

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-08-02 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (55 votes), past polls