I think the only changes I would make involve a few trivial simplifications:
sub list {
my @words = split( /\s+/, $_[0] );
my $lastword = $#words;
for my $i (1..$lastword) {
push @words, map { join " ", @words[$_..$_+$i] } (0..$lastword
+-$i);
}
return @words;
}
Notes:
- Assuming that $/ has its default value, splitting on white-space makes "chomp" unnecessary
- Your map-within-foreach covers everything, including the "substring" containing all input words
- You can eliminate most of your temporary storage variables, but ...
- ... it's important not to use "$#array" inside a loop when altering the size of @array in the loop (unless you're doing something different from this case, and you really know what you're doing)
(update: fixed @_[0] --> $_[0] in first line of sub -- and this could just as well have been "shift".)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|