CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
regular expression, white spaces

 



lukdk
Novice

Aug 27, 2012, 5:19 AM

Post #1 of 5 (880 views)
regular expression, white spaces Can't Post

Hello,



I'm trying to remove the white spaces in front and after a variable. The variable comes from a file which needs to be read. The following code seems to work at first:

sub trim{
my $str=shift;
return "" if !defined $str;
$str=~s/^\s+//;
$str=~s/\s+$//;
return ($str);
}

However, when I try this code, it doesn't remove anything. I tried to open the input file in a text editor and saw white spaces in front and after the string. When I tried in a dos box "type input.txt" I noticed all the white spaces got replaced by "" signs.

I'm guessing I'll need to use another expression, the problem is I don't know what to match for. How can I discover that hidden character that seems like a white space, but isn't one after all?


shawnhcorey
Enthusiast


Aug 27, 2012, 6:18 AM

Post #2 of 5 (878 views)
Re: [lukdk] regular expression, white spaces [In reply to] Can't Post

How are you calling trim()?

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


lukdk
Novice

Aug 27, 2012, 9:00 AM

Post #3 of 5 (873 views)
Re: [shawnhcorey] regular expression, white spaces [In reply to] Can't Post

Hello,

I don't think my problem is the way i'm calling the sub:

See this example from notepad:

" " <= this is a space
" " <= this is not a space

however, when i print it out in a dos prompt i get this:

C:\>type temp.txt
" " <= this is a space
"" <= this is not a space


Laurent_R
Veteran / Moderator

Aug 27, 2012, 10:48 AM

Post #4 of 5 (865 views)
Re: [lukdk] regular expression, white spaces [In reply to] Can't Post

The best would be to use an hexadecimal editor to find out what character you have there.

Or you can use Perl to print separately the ASCII value of each letter.


BillKSmith
Veteran

Aug 27, 2012, 12:15 PM

Post #5 of 5 (862 views)
Re: [lukdk] regular expression, white spaces [In reply to] Can't Post

The type command is telling you that the file contains non-ASCII characters. Before attempting to get rid of them, you really should figure out where they come from. Two possibilities come to mind. The first is that the file is not intended to be an ASCII file. In that case you must either tell perl the encoding or read the file in binary and convert it yourself. A second possibility is that you are reading past the end of the file. This is very unlikely if the file is being read on the same machine that created it. The problem can be hard to track down unless you know (for sure) the last few characters of some file or its exact length. If neither of these are the cause, I strongly recommend that you keep looking for it. Blindly removing data that you do not understand is going to cause problems in the future.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives