To use regular expressions to chop leading spaces, use the ^ anchor to specify 'start at the beginning'. Also, rather than the g modifier (since you really only need to substitute once), use + to indicate 'one or more'. I prefer the character class \s to an explicit space (' '), but YMMV.
s/^\s+//
YAPE::Regex::Explain describes this as:
The regular expression:
(?-imsx:^\s+)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
See Metacharacters and Quantifiers in perlre.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
| [reply] [d/l] [select] |
This article seems to most clearly explain various methods of removing whitespace from the start and end of strings.
If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)
| [reply] |
Is the space actually in the array rows, or is it just being added when you print the array ?
I ran into this recently, and found out that the spaces weren't actually in the array.
If you're printing the array with something like:
print "@TheArray";
and you don't have an extra space in front of the first line, but you do have an extra space in front of all other lines, the spaces probably aren't in the array.
Perl by default seperates the array with spaces in an interpolative context (meaning if you have the array inside double quotes). That's why the first row doesn't have a space in front of it - the first row doesn't need to be seperated from another row in front of it, because it's the first row.
If you print with something like this, the extra spaces won't show up:
print @TheArray;
Without the "", Perl doesn't interpolate the array, meaning it doesn't seperate the rows with spaces.
The following script line also works, but is more verbose. I tend to write scripts that are more verbose, because I'm not a Perl Master yet :)
foreach $ArrayRow( @TestArray ) { print $ArrayRow; }
Here's a demo script:
#!C:\Perl\bin
#use warnings;
#use strict;
my $ArrayRow = "";
splice( my @TestArray );
$ArrayRow = "20130516,530,730\n";
push( @TestArray, $ArrayRow );
$ArrayRow = "20130516,731,1100\n";
push( @TestArray, $ArrayRow );
$ArrayRow = "20130516,1101,1200\n";
push( @TestArray, $ArrayRow );
print "@TestArray";
print "\n\n";
print @TestArray;
print "\n\n";
foreach $ArrayRow( @TestArray ) { print $ArrayRow; }
print "\n\n";
| [reply] [d/l] |
| [reply] |