Further experiments:
#!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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.