On Sat, 2018-03-10 at 00:14 +0100, hw wrote:
> 
> Not really, the words I get from the ReadLine functions are not
> organized in an array.
> 
> I´ve come up with this function to compute the "depth" of a word:
> 
> 
> sub get_depth {
>   my $string = shift;
> 
>   $string =~ s/\s/ /g;
>   $string =~ s/\s+/ /g;
>   $string =~ s/^\s*//;
>   $string =~ s/\s*$//;
> 
>   my $depth = 0;
>   my $position = 0;
>   while((my $start = index($string, ' ', $position)) != -1) {
>     $position = $start + 1;
>     $depth++;
>   }
> 
>   return $depth;
> }

That function could be simplified to:

sub get_depth {
    my $string = shift;

    $string =~ s/^\s+//;
    $string =~ s/\s+$//;
    my $depth = $string =~ s/\s+/ /g;

    return $depth;
    }


John

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to