#!usr/bin/perl use strict; use warnings; print $0 . "\n\n"; my @var= ('now is the time', # space, tab between now and i +s, 4 in entire line (3 spaces & 1 tab) ' for all good men', # leading tab, no space, 4 + in entire line (1 tab & 3 spaces) 'to come to the aid of their party.' # space, tab before + party, 8 (7 spaces & 1 tab) ); # Sixteen to +tal spaces & tabs # now using \s & /g print "and now using \\s only, with /g modifier\n"; my $count_s = 0; my $linecount_s = 0; for my $var(@var) { $linecount_s = scalar ( $var =~ s/\s/_/g ); print "\$linecount_s: $linecount_s, \$var after substitution: $var +\n"; $count_s += $linecount_s; } print "\$count_s: $count_s \n\n"; # and now using \s and \t print "Now, simple count (Note new array \@var1) on LEADING \\s or \\t +:\n"; my @var1 = (' Now is the time', #NO leading spaces, two +leading tabs ' for all men to come to the', #one leading space ' of their party.' #5 leading spaces ); my $matchcount = 0; for my $var1(@var1) { if ( $var1 =~ /^(\s*)/ && length($1) ) { $matchcount += length($1); } } print "Total \$matchcount: $matchcount (of tabs and spaces)"; __END__ =head OUTPUT from countspaces2.pl (initially using the same @var as in + the previous node) and now using \s only, with /g modifier $linecount_s: 4, $var after substitution: now__is_the_time $linecount_s: 4, $var after substitution: _for_all_good_men $linecount_s: 8, $var after substitution: to_come_to_the_aid_of_their_ +_party. $count_s: 16 # Contrast the result from countspaces.pl (previous no +de) # Correct count and "_" replaced each "\t" Now, simple count (Note new array @var1) on LEADING \s or \t: Total $matchcount: 8 (of tabs and spaces) # Correct =cut
In reply to Re^2: counting leading white spaces
by ww
in thread counting leading white spaces
by Spooky
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |