Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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 avoiding work at the Monastery: (4)
As of 2015-07-05 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls