Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Preceding Spaces

Status
Not open for further replies.

jonnymp316

IS-IT--Management
Jun 6, 2003
6
US
Hello,
I am inputing text from a file like so:

my $n = 1;
open INFO, '<stored.txt';
my @stuff = <INFO>;
foreach (@stuff){
print $stuff[$n];
if (n < 30) {
$n++
}
}
close INFO;

The problem is that the variable that I am reading from the text file has 12 preceding spaces, I was just wondering if anyone knows how I can parse out those preceding spaces, like the opposite of the chop/chomp command.

Thanks,
-Jonny
 
That's a mighty odd way of doing a loop, but a simple regex can take it out. Can take out both leading and trailing whitespace, if you'd like. Example:
Code:
my $line = &quot;     Five spaces before, and a space and newline terminating \n&quot;;
$line =~ s/^\s+|\s+$//g;
If efficency is a big issue, generally separating out an OR regex into two separate ones is faster, though I don't know if that still applies when using anchors on both.
Code:
$line =~ s/^\s+//g;
$line =~ s/\s+$//g;

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top