You need a very clear description of what it should do. A word can mean a whole heap of different things - 2 bytes - delimited by whitespace and containing at least one alpha or numeric character ... etc.
If it's the common or garden "delimited by cr or space", then I'd use split on a regexp to mean "1 or more of: ( space or carriage return)" (I'll follow the above trend of letting you read the docs rather than just giving the answer). You can find more about regexps in perlre.
One other point though: when reading from a file with the <> operator, what is read is determined by scalar or list context (scalar read enough for a scalar, list read whole file into a list or array) together with whatever is in the $/ variable (the record delimiter). Thus to count the number of fields in STDIN, delimited by a regexp ...
use strict;
use warnings;
$/=undef(); # make it read everything in the file into a scalar contex
+t
my @words = split /the regexp/, <>;
# now the size of the array given by $#words is one less than the word
+count
__________________________________________________________________________________
^M Free your mind!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.