tags:

views:

84

answers:

6

How can I count the amount of spaces at the start of a string in Perl?

I now have:

  $temp = rtrim($line[0]);
  $count = ($temp =~ tr/^ //);

But that gives me the count of all spaces.

+7  A: 
$str =~ /^(\s*)/;
my $count = length( $1 );

If you just want actual spaces (instead of whitespace), then that would be:

$str =~ /^( *)/;

Edit: The reason why tr doesn't work is it's not a regular expression operator. What you're doing with $count = ( $temp =~ tr/^ // ); is replacing all instances of ^ and with itself (see comment below by cjm), then counting up how many replacements you've done. tr doesn't see ^ as "hey this is the beginning of the string pseudo-character" it sees it as "hey this is a ^".

CanSpice
thanks, this works perfect!
edelwater
@CanSpice, `tr` doesn't delete characters unless you use the `/d` modifier. The original code was actually replacing `^` and space with `^` and space (if the REPLACEMENTLIST is empty, the SEARCHLIST is replicated(http://perldoc.perl.org/perlop.html#tr%2fSEARCHLIST%2fREPLACEMENTLIST%2fcds).)
cjm
Ah yes, thanks for the correction. I've edited my post to reflect reality.
CanSpice
A: 

Here's a script that does this for every line of stdin. The relevant snippet of code is the first in the body of the loop.

  #!/usr/bin/perl

  while ($x = <>) {
    $s = length(($x =~ m/^( +)/)[0]);
    print $s, ":", $x, "\n";
  }
Chris Cleeland
oops. looks like concurrent similar answers.
Chris Cleeland
+1  A: 

tr/// is not a regex operator. However, you can use s///:

use strict; use warnings;

my $t = (my $s = " \t\n  sdklsdjfkl");
my $n = 0;

++$n while $s =~ s{^\s}{};
print "$n \\s characters were removed from \$s\n";

$n = ( $t =~ s{^(\s*)}{} ) && length $1;
print "$n \\s characters were removed from \$t\n";
Sinan Ünür
That will be destructive on the original string, so make sure to assign to a separate variable if you want to retrieve the original value later!
Platinum Azure
+3  A: 

You can get the offset of a match using @-. If you search for a non-whitespace character, this will be the number of whitespace characters at the start of the string:

#!/usr/bin/perl

use strict;
use warnings;

for my $s ("foo bar", " foo bar", "  foo bar", "  ") {
        my $count = $s =~ /\S/ ? $-[0] : length $s;
        print "'$s' has $count whitespace characters at its start\n";
}

Or, even better, use @+ to find the end of the whitespace:

#!/usr/bin/perl

use strict;
use warnings;

for my $s ("foo bar", " foo bar", "  foo bar", "  ") {
    $s =~ /^\s*/;
    print "$+[0] '$s'\n";
}
Chas. Owens
+1. This is nice.
codaddict
A: 

Since the regexp matcher returns the parenthesed matches when called in a list context, CanSpice's answer can be written in a single statement:

$count = length( ($line[0] =~ /^( *)/)[0] );
Martin Sojka
A: 

This prints amount of white space

echo "   hello" |perl -lane 's/^(\s+)(.*)+$/length($1)/e; print'

3

sfgroups