Trim() does not work on the first line...

Just starting out? Need help? Post your questions and find answers here.
highend
Enthusiast
Enthusiast
Posts: 125
Joined: Tue Jun 17, 2014 4:49 pm

Trim() does not work on the first line...

Post by highend »

Hi,

I'm reading an UTF-8 BOM file line by line and one thing that
needs to be done is trimming spaces and tabs from the beginning
and end of all lines

Code: Select all

    While Not Eof(fileHandle)                 ; Loop as long the 'end of file' isn't reached
      line = ReadString(fileHandle)          ; Get the line
      line = Trim(line)                           ; Trim spaces
      line = Trim(line, #TAB$)                ; Trim tabs as well
      Debug "#"+line+"#"
      ; Do more...
    Wend
The file begins with this:

Code: Select all

		::
		Set $Bookmarks;
These lines are leaded by two tabs

The debug output looks like this:

Code: Select all

#		::#
#Set $Bookmarks;#
Why aren't the two tabs from the first line not removed by

Code: Select all

line = Trim(line, #TAB$)
?
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Trim() does not work on the first line...

Post by Josh »

If there is first a Tab and then a Space, the Space will not be eliminated

or

there is a BOM in your file
sorry for my bad english
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Trim() does not work on the first line...

Post by IdeasVacuum »

Even if you are not interested in the file format, use ReadStringFormat() first, which moves the file read pointer beyond the BOM (Byte Order Mark) marker. Then Trim tabs, spaces or anything else that gets in the way. :D
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Trim() does not work on the first line...

Post by Little John »

If you are not sure whether a line begins with a Space or with a Tab, you can use the functions which I posted here.

Then your code could look like this:

Code: Select all

fmt = ReadStringFormat(fileHandle)
While Not Eof(fileHandle)
   line$ = TrimAny(ReadString(fileHandle))
   ; [...]
Wend
highend
Enthusiast
Enthusiast
Posts: 125
Joined: Tue Jun 17, 2014 4:49 pm

Re: Trim() does not work on the first line...

Post by highend »

OK,

I've tried it with

Code: Select all

FileSeek(fileHandle, 4)
which worked as well but ReadStringFormat(#File)
really seems to be the simpler option here.

@Little John
My first try was to read the file into memory in one go
and do most of the necessary processing by ~2 regex replaces
(including the other work I need to do...)
but it seems I can't use ReplaceRegularExpression() on a string
in memory... (with something like @*buffer). I'll take a look
at your procedures.

Thanks!
Last edited by highend on Wed Aug 23, 2017 9:19 pm, edited 1 time in total.
normeus
Enthusiast
Enthusiast
Posts: 415
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Trim() does not work on the first line...

Post by normeus »

@IdeasVacuum has the simplest solution since I see a BOM on the sample you pasted:
0xEF

Code: Select all

ReadStringFormat(fileHandle)             ;as IdeasVacuum suggested  
    While Not Eof(fileHandle)                 ; Loop as long the 'end of file' isn't reached
      line = ReadString(fileHandle)          ; Get the line
      line = Trim(line)                           ; Trim spaces
      line = Trim(line, #TAB$)                ; Trim tabs as well
      Debug "#"+line+"#"
      ; Do more...
    Wend
@Little John's TrimAny() function looks useful so I will save that link for me.


Norm.
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Trim() does not work on the first line...

Post by IdeasVacuum »

Unless you always know how many bytes to jump, don't use FileSeek()
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply