Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Count Quoted Words

by Anonymous Monk
on Jun 07, 2013 at 02:31 UTC ( #1037552=note: print w/ replies, xml ) Need Help??


in reply to Count Quoted Words

Mwahahahaha

#!/usr/bin/perl -- #~ #~ #~ #~ # perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -op +r -ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use autodie; # error checking for open/close... Main( @ARGV ); exit( 0 ); sub Main { my( @files ) = @_; if( not @files ) { my $lines = q{ And "then" something "happened" on "the first and second and third lines" and then it was over}; @files = ( \'"the boat"', \$lines, \"$lines $lines $lines" ); } for my $file ( @files ) { print "## { $file }{ wordcount= ", Vote( $file ), " }\n"; } } ## end sub Main sub Vote { my( $fyle ) = @_; open my( $wyld ), '<:raw', $fyle; my $in_quotes = 0; my $words = 0; while( my $line = readline $wyld ) { pos( $line ) = 0; WORDCOUNTER: while( length( $line ) > pos( $line ) ) { $line =~ m{ \G\s*\x22 # quote after optional whitespace }gxcs and do { $in_quotes = !$in_quotes; ## flip it next WORDCOUNTER; }; $in_quotes and $line =~ m{ [^\x22\s]+ ## not quote or whitespace }gxcs and do { $words++; next WORDCOUNTER; }; $line =~ m{ \G[^\x22]+ ## not quote }gxcs and do { next WORDCOUNTER; }; } ## end of WORDCOUNTER } ## end of readline return $words; } ## end sub Vote __END__ ## { SCALAR(0xbb713c) }{ wordcount= 2 } ## { SCALAR(0xad05a4) }{ wordcount= 9 } ## { SCALAR(0x3f8fec) }{ wordcount= 27 }


Comment on Re: Count Quoted Words
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (16)
As of 2014-08-22 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls