$i=0; do{ ... $i++ } until ; my $x = substr $somestring, 0, $i;