anita14 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: perl prog to read a sentence and print the count of chars for each word
by toolic (Bishop) on Feb 20, 2012 at 21:29 UTC
    Your Perl installation includes documentation and even a searchable FAQ, all at your command line:

    Read up, write some code, and post back here if you have specific questions.

Re: perl prog to read a sentence and print the count of chars for each word
by tobyink (Canon) on Feb 20, 2012 at 23:59 UTC
    use 5.010; say "Enter a sentance:"; say join q{ }, # join each with a space, and say map { length $_ } # find length grep { $_ } # ignore zero-length map { s{\W}{}g; $_ } # strip out non-word characters split m{\s+}, # split on whitespace do { my $line = <> }; # read a line __END__ Enter a sentance: A man, a plan, a canal: Panama 1 3 1 4 1 5 6
Re: perl prog to read a sentence and print the count of chars for each word
by ricDeez (Scribe) on Feb 20, 2012 at 21:29 UTC

    So what exactly have you tried so far? While we are glad to help, this looks like 'homework' so if you want some help you will need to show us what you have tried, what worked and what didn't work. This is a very trivial application for Perl and something that you should be able to tackle after reading the first few chapters of Learning Perl.

    Please amend your post and we'll try to help...

      #!/usr/strawberry/perl/bin print ("Enter a sentence \n"); $count = 1; $str = <STDIN>; chop ($str); @array = split (' ', $str); while ($count <= @array){ $word = $array $count - 1; $lengthWord = length($word); print (" ", $lengthWord); $count++; }
        • Please reformat your post using code tags: Writeup Formatting Tips
        • Please accompany your code with some text. Why did you post this code? Does it work the way you expect? Do you want a critique of the style?
Re: perl prog to read a sentence and print the count of chars for each word
by jwkrahn (Abbot) on Feb 20, 2012 at 22:41 UTC
    $ perl -le' my $input = "I am new to perl"; while ( $input =~ /\S+/g ) { print $+[0] - $-[0]; } ' 1 2 3 2 4
Re: perl prog to read a sentence and print the count of chars for each word
by JavaFan (Canon) on Feb 20, 2012 at 23:57 UTC
    echo "I am new to perl" | perl -anEsay+y+++c\ for@F
Re: perl prog to read a sentence and print the count of chars for each word
by rovf (Priest) on Feb 21, 2012 at 09:59 UTC
    And your Perl does not complain at the statement

    $word = $array $count - 1;
    ????

    BTW, you forgot to set use strict; use warnings; in your program.
    -- 
    Ronald Fischer <ynnor@mm.st>