Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: finding number of contiguous letters

by Zaxo (Archbishop)
on May 23, 2007 at 04:41 UTC ( #616927=note: print w/replies, xml ) Need Help??

in reply to finding number of contiguous letters

I'm starting to think that I might need to use a while loop that uses a counter to substring the word and then increment its starting point by one until the end of the string.

That was my first thought, and probably how I'd do it. Here's another way that might entertain you,

local $_ = 'computer'; my @results; pos() -= 2, push @results, $1 while /(...)/g; print "@results", $/;
We need to keep subtracting one less than the length of the match from pos to match the overlapping strings.

After Compline,

Replies are listed 'Best First'.
Re^2: finding number of contiguous letters
by otto (Beadle) on May 23, 2007 at 16:13 UTC

    Far better solutions have been presented, but here is another. I was considering how to do if we had tail recursion. I had been hoping to place all code in the argument list...

    sub parts { my ( $cnt, $ra_out, $ra_val, $ra_in ) = @_; if( $#$ra_val != $cnt ){ #working array not full if( $#$ra_in > -1 ){ #fill it up push(@$ra_val, shift(@$ra_in)); parts( $cnt, $ra_out, $ra_val, $ra_in ); } else { #done with list return; } } else { push(@$ra_out, join('',@$ra_val)); #add to outp +ut array, joined ltr string shift(@$ra_val); #rip off fir +st ltr push(@$ra_val, shift(@$ra_in)) if($#$ra_in > -1); #add new ltr +, if any remaining parts( $cnt, $ra_out, $ra_val, $ra_in ); } } my @in = split( '', "computer" ); # input array of individual letters my @out; # out array of strings each of $cnt ++1 ltrs my @val; # working array of individual lette +rs, $cnt+1 elements parts( 2, \@out, \@val, \@in ); #results in @out


      I was considering how to do if we had tail recursion.

      We do have tail recursion. Well not really: sort of. That's courtesy of magic goto: but then of course one has to explicitly make use of it, not that TR is automatically recognized, which is what you clearly meant.


        You got me thinking, I'm going to play with this some more, but first... are sub arguments, ie a list, evaluated in order?

        For example suppose a list is composed of ( <a>, <b>, <c> )where each of the a, b, c are one or more sub calls that may mutate some value, which may be used by others further down the list, will the list values be evaluated in order of a, b, c, or is it undefined?

        I quickly looked at the sub function and the comma operator which did not shed any light, or at least I didn't see it...

        .. Otto

Re^2: finding number of contiguous letters
by Marknel (Acolyte) on May 23, 2007 at 04:50 UTC
    off I go to learn about pos!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://616927]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2020-10-27 21:18 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (258 votes). Check out past polls.