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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing the leading white space in a string 1

Status
Not open for further replies.

getbabs

Programmer
Jan 30, 2002
3
0
0
US
Hi All,

I have a string in the below format :

$ad = " 0 600 600 ";

I need to remove the leading white space
and split this string in to three values as
0,600,600.

Thanks in Advance.
 
Try this:
Code:
push @values, $1 while($ad=~/(\d+)/g);

@values now contains all the values in the string.

--G
 
I'm surprised that works. I thought @values would end up with an empty first element, which is what you get if you say

@values = split(/\s+/, $ad);

Checking the documentation, I see that it says
If EXPR is omitted, splits the $_ string. If PATTERN is also
omitted, splits on whitespace (after skipping any leading
whitespace)
.
So it looks like you don't get the empty first element only if both args to split are omitted. Which means you must be splitting $_ on whitespace. Otherwise I guess you would.
I didn't know that. Thanks.
 
You can also use the pattern of a single space and split will skip any leading spaces. So...

Code:
$ad = "     0    600    600 ";
@values = split ' ', $ad;

works like Jim's code except you don't need to work exclusively on $_. I think I remember reading that split sans args does split(' ', $_).
 
Thanks rharsh, I was fiddling with that trying to see how to do it without $_ and I gave up after a few tries, using undef and stuff like that. Didn't think about just using a space... I did try / /, but that yeilds quite different results. SO it can't be a pattern of a space, it has to be a literal string (I don't say 'literal' by accident - read on). AND I was curious so I assigned ' ' to a variable and then split on that - it acts as a pattern. So I am curious enough that I might check out the perl-source-code that does this, becuase it appears that perl requires a literal-string, not a variable that would hold the equivelent value. Is there special parsing going on?

Knowing that little "perl gotcha" is another notch in our belts.

Good stuff.

--jim
 
I tracked down where I read about this behaviour of split. Here's the quote from perlfunc:

As a special case, specifying a PATTERN of space (' ') will split on white space just as split with no arguments does. Thus, split(' ') can be used to emulate awk's default behavior, whereas split(/ /) will give you as many null initial fields as there are leading spaces. A split on /\s+/ is like a split(' ') except that any leading whitespace produces a null first field. A split with no arguments really does a split(' ', $_) internally.

Jim, if you take the time to go through the source, post what you find. I'd be interested to see how it works as well but don't have much time to dig.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top