I modified your code a little to fit the problem description slightly better:
use strict; my $num_words = 4; my @words = (); open FILE, "<in.txt" or die $!; while ( <FILE> ) { last if ( push ( @words, m/\s*([^\s]+)\s*/g ) >= $num_words ); } close FILE; print join( " ", @words ) . "\n";
As a side note, is there a reason to use [^\s] rather than \S, or is it just a matter of preference? Thanks.
Zenon Zabinski | zdog | zdog@perlmonk.org
In reply to Re^2: Counting words
by zdog
in thread Counting words
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |