Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Counting words

by jbware (Chaplain)
on Sep 13, 2004 at 14:40 UTC ( [id://390569]=note: print w/replies, xml ) Need Help??


in reply to Counting words

Here is a regex way to grab the first x words for each line (in my example x=4).
use strict; open (IN,"<in.txt") or die $!; while (<IN>) { print "$1\n" if (/^\s*((?:[^\s]+\s+){4})/); } close(IN);

-jbWare

Replies are listed 'Best First'.
Re^2: Counting words
by zdog (Priest) on Sep 13, 2004 at 15:26 UTC

    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

      Yeah, "last if" is a good call, I wasn't thinking. The OP wasn't clear exactly how they wanted the results back (string or array of words), so I choose string. Nice convert to array though.

      As far as [^\s] versus \S, force of habit. If my laziness virtue would kick in like its supposed to, I'd have switched to \S by now and saved some keystrokes :)

      -jbWare

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://390569]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-04-23 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found