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

Checking for spaces... 2

Status
Not open for further replies.

Loon

Programmer
May 24, 2000
100
GB
Hey there,<br><br>as part of a script I read a line of text into several fields using substr() this is fine except that sometimes not all fields are present on a line. Instead there are space characters.<br><br>Therefore I thought to use a substitution after I had split the string up to get rid of the whitespace:<br><br><FONT FACE=monospace>$thisfield =~ s/ //;</font><br><br>However when I then check the field:<br><br><FONT FACE=monospace>if (!($thisfield eq &quot;&quot;)) { do something with a non-empty string }</font><br><br>This doesn't work as even though I thought I'd got rid of the spaces $thisfield still shows up as a defined string..<br><br>Any ideas (better ways to do this?) appreciated!<br><br>Cheers<br>Loon
 
two things maybe...<br>one - you may need to use a 'g' on the end of your replace.&nbsp;&nbsp;The way you have it, you will replace the first and only the first space.<br><br>two - you may have white space chars other than a 'space' , like tabs or carriage returns or other.<br><br>' might try this..... <br><b>$thisfield =~ s/\s+//g;</b><br><br>that will replace any one or more white space chars (spaces, tabs, new lines, etc...) with null.
 
That did the trick!<br><br>Many thanks<br>Loon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top