Page 1 of 1

LTrim Questions ? Problems

Posted: Mon Oct 19, 2009 5:28 pm
by John-G
I have a incoming String 12 chr long. I have been trying to handle this and it is not working the way i though it would :roll: Could i be doing something wrong
If do this it works

A$="00050140000;"
LTrim(A$,"0")
debug A$

Debug prints out 00050140000
but if i do it this way

a$ = LTrim("0050140000;","0")
debug a$

Why and if so how do i do it ... thanks John

Re: LTrim Questions??? Problems

Posted: Mon Oct 19, 2009 5:41 pm
by ts-soft
the first one is false, should use in this way:

Code: Select all

A$="00050140000;"
A$ = LTrim(A$,"0")
Debug A$
There is no inplace change :)

greetings
Thomas

Re: LTrim Questions??? Problems

Posted: Mon Oct 19, 2009 6:09 pm
by John-G
Ok but i am reading this from the RS232 and take the input in to A$ . Now i want to Show A$ to the User and i would like to clean off the 0000 at the start and the ";" at the end .. so i need to edit the string ... any thoughs?


ts-soft wrote:the first one is false, should use in this way:

Code: Select all

A$="00050140000;"
A$ = LTrim(A$,"0")
Debug A$
There is no inplace change :)

greetings
Thomas

Re: LTrim Questions??? Problems

Posted: Mon Oct 19, 2009 6:17 pm
by ts-soft
Use different vars:

Code: Select all

A$="00050140000;"
B$ = LTrim(A$,"0")
Debug B$
Debug A$
or i have misunderstand your problem

Re: LTrim Questions??? Problems

Posted: Mon Oct 19, 2009 6:31 pm
by John-G
It might be me the way i was asking ... here is the Code that i am using. It work grate the only problem i have is that i would like to remove all leading 00000000000 and on the end of the string the ";" from the end. The Text2.s ends up with
"0050140000;" i wanted to clean it up look for "0" at the start could be 4,3,2,1 or none and the ";" from the end just would look better .. NOW any thoughs? :?

Code: Select all

result=WriteSerialPortString(1, "FA;"+Chr(13))

      Delay(100)

      x=AvailableSerialPortInput(1)

      Delay(100)

      Text2.s =""

      Buffer$=""

      While AvailableSerialPortInput(1)>0

        If ReadSerialPortData(1, @Buffer, 1) ; Read Byte

          Text2.s = Text2.s + Chr(Buffer)

        EndIf

      Wend

Re: LTrim Questions??? Problems

Posted: Mon Oct 19, 2009 7:48 pm
by cas
John-G wrote: i wanted to clean it up look for "0" at the start could be 4,3,2,1 or none and the ";" from the end just would look better .. NOW any thoughs? :?
Something like this(?):

Code: Select all

Procedure.s cleanup(Text.s)
  Protected number_of_zeros=0,i=0
  For i=0 To Len(Text.s)
    If Mid(Text.s,i,1)="0"
      number_of_zeros+1
    Else
      Break
    EndIf
  Next
  If number_of_zeros=0 : number_of_zeros+1 : EndIf
  
  ProcedureReturn Mid(Text.s,number_of_zeros,Len(Text.s)-number_of_zeros)
EndProcedure

Text.s = "0050140000;"

AfterCleanup.s = cleanup(Text.s)

Debug AfterCleanup.s
EDIT:
actually, there is easier way:

Code: Select all

Text.s = "0050140000;"

AfterCleanup.s = Str(Val(Text.s))

Debug AfterCleanup.s

Re: LTrim Questions??? Problems

Posted: Mon Oct 19, 2009 8:28 pm
by John-G
Thanks .. I knew there was a quick way ... that is want i needed .. Again thanks ..

Re: LTrim Questions??? Problems

Posted: Tue Oct 20, 2009 2:28 pm
by John-G
Working verry good .. but one more problem i need to put a point every 3 letters counting from the right to the left

50.140.000
144.225.000

Any thoughts? :?

Re: LTrim Questions??? Problems

Posted: Tue Oct 20, 2009 4:48 pm
by blueznl

Code: Select all

a.s = "1234"
b.s = ""
p = Len(a.s)
While p > 0
  b = Mid(a,p,1)+b
  If (p+1) % 3 = 0
    b = "."+b  
  EndIf
  p = p-1
Wend
Debug b.s

Re: LTrim Questions??? Problems

Posted: Tue Oct 20, 2009 4:57 pm
by moogle
John-G wrote:Working verry good .. but one more problem i need to put a point every 3 letters counting from the right to the left

50.140.000
144.225.000

Any thoughts? :?

Code: Select all

number$="1123456789"
Procedure.s FormatNum(string$, formatchar$=",")
	length=Len(string$)
	loops=Round(length/3, #PB_Round_Up)
	For i=1 To loops
		If length-(3*i)+1 <= 0
			lenr=length-(3*i)+3
		Else
			lenr=3
		EndIf
		If i <> loops
			temp$=formatchar$+Mid(string$,length-(3*i)+1,3)+temp$
		Else
			temp$=Mid(string$,length-(3*i)+1,lenr)+temp$
		EndIf
	Next
	ProcedureReturn temp$
EndProcedure


Debug FormatNum(number$)
Some very rushed code that seems to work :)

Re: LTrim Questions??? Problems

Posted: Tue Oct 20, 2009 5:56 pm
by #NULL
although the problem seems to be solved already, here is what i had just on my disc :) ..but it's for number input, not for strings.

Code: Select all

; formatByteSize.pbi

; changelog:
; StrQ() -> Str() [pb430b4]


Procedure.s formatByteSize(n.q)
  Protected s.s=Str(n)
  Protected len=Len(s)
  Protected ret.s
  Protected i.l
 
  For i=0 To len-1
    If i And Not i%3 :: ret="."+ret :: EndIf
    ret= Mid(s,len-i,1) +ret
  Next
 
  ProcedureReturn ret
EndProcedure


; [merge_end]





;  ;[another version with optional seperator selection and check for effectiveness]
; 
; Procedure.s formatByteSize( n.q, sep.s="." )
;   If n<1000
;     ProcedureReturn Str(n)
;   Else
;     Protected s.s=Str(n)
;     Protected len=Len(s)
;     Protected ret.s
;    
;     For i=0 To len-1
;       If i And Not i%3 :: ret=sep+ret :: EndIf
;       ret= Mid(s,len-i,1) +ret
;     Next
;    
;     ProcedureReturn ret
;   EndIf
; EndProcedure













; Debug "---- EXAMPLE - formatByteSize.pbi"
; Debug formatByteSize(1)
; Debug formatByteSize(12)
; Debug formatByteSize(123)
; Debug formatByteSize(1234)
; Debug formatByteSize(12345)
; Debug formatByteSize(123456)
; Debug formatByteSize(1234567)
; Debug formatByteSize(12345678)
; Debug formatByteSize(123456789)
; Debug formatByteSize(1234567890)
; Debug formatByteSize(4294967295)
; Debug ""
; DisableDebugger
; t=ElapsedMilliseconds()
; For i=0 To 500000
;   formatByteSize(i)
; Next
; t=ElapsedMilliseconds()-t
; EnableDebugger
; Debug t
; Debug "----"









Re: LTrim Questions??? Problems

Posted: Tue Oct 20, 2009 7:47 pm
by TerryHough
This Windows API example may give you a bit more control.

Code: Select all

; -FYI - NUMBERFMT Structure
; Structure NUMBERFMT
;   NumDigits.l
;   LeadingZero.l
;   Grouping.l
;   lpDecimalSep.l
;   lpThousandSep.l
;   NegativeOrder.l
; EndStructure
Global NumberF.NUMBERFMT

;-Preferred Number Format - Set up my preferred number format settings
Global Thousand$ = ","
Global Decimal$  = "."

NumberF\NumDigits         = 0 ; No of digits following the decimal  <---- set as desired
NumberF\LeadingZero       = 0 ; Value   Meaning                     <---- set as desired
                              ;     0   No leading zeros
                              ;     1   Leading zeros
NumberF\Grouping          = 3 ; How many digits in each group       <---- set as desired
NumberF\lpDecimalSep      = @Decimal$
NumberF\lpThousandSep     = @Thousand$
NumberF\NegativeOrder     = 2 ; Value   Meaning                     <---- set as desired
                              ;     0   (1.1)
                              ;     1   -1.1
                              ;     2   - 1.1
                              ;     3   1.1-
                              ;     4   1.1 -
; -FYI - FORMATTING Structure for internal use

A$ = "00000561400;"
Buffer.s = Space(256)
result = GetNumberFormat_(#LOCALE_SYSTEM_DEFAULT, 0, Str(Val(A$)), @NumberF, @Buffer, Len(Buffer))
If result 
  MessageRequester("Results returned",B$ + #LF$ + Buffer, #MB_ICONINFORMATION)
Else
  error = GetLastError_()
  Select error
    Case #ERROR_INSUFFICIENT_BUFFER
      Msg$ = "Insufficent buffer space"
    Case #ERROR_INVALID_FLAGS
      Msg$ = "Invalid flags"
    Case #ERROR_INVALID_PARAMETER
      Msg$ = "Invalid parameter"
  EndSelect    
  MessageRequester("Error",Msg$, #MB_ICONERROR)
EndIf
End