in reply to text parsing question

Possibly the least imaginitive and most expensive solution I can think of is to loop. This is clunky... but it works!

my $str = "- -- --- ---- -----";
$str =~ s/^( +)//; # how many cliches does it take to
my $cnt = length $1; # hit the ground running
my $last1 = "-"; # for the sake of argument

while ($str =~ s/(.)//)
{
   push @ends, $cnt if ($last1 eq "-") && ($1 eq " ");
   $last1 = $1;
   $cnt++;
}

push @ends, $cnt if $last1 eq "-";
print join (", ", @ends);

Replies are listed 'Best First'.
Re^2: text parsing question
by tachyon (Chancellor) on Nov 07, 2004 at 23:04 UTC

    If you are going to do it like you would in C.....

    use Inline 'C'; my $string = ' ----------- ------'; my @res = parse($string); print "@res\n"; __END__ __C__ void parse ( char * str ) { int i = 0; char cur, last = '\0'; dXSARGS; sp = mark; while ( cur = *(str++) ) { if ( last == '-' && (isspace(cur) || ! *str) ) XPUSHs(sv_2mortal(newSViv(i))); last = cur; i++; } PUTBACK; }

    cheers

    tachyon