Author Topic: Funktion gibt String - Variable mit "\" nicht zurück, aus \\ wird \  (Read 827 times)

Offline uwe_gl

  • Mc. Print
  • *
  • Posts: 5
    • View Profile
Hallo,
Eine Funktion zum ersetzen der "\" mit "/" (zum Dateinamenumwandeln für SHELLCMD) gibt keinen Wert zurück wenn der String Sonderzeichen wie "\" oder "/" enthält. Es wird auch nur ein "\" in der Funktion umgewandelt, das 2. verschwindet ....

Code: GLBasic [Select]
GLOBAL a$="Test\\//Test"
PRINT a$,10,30
PRINT KONV(a$),10,100
SHOWSCREEN
MOUSEWAIT

END


// ------------------------------------------------------------- //
// ---  KONV  ---
// ------------------------------------------------------------- //
FUNCTION KONV: wert1$
        // Diese Variablen sind als LOCAL definiert:
        // wert1$
LOCAL wert2$

FOR t=0 TO LEN(wert1$)-1
        IF MID$(wert1$,t,1)="/"
                wert2$=wert2$+CHR$(92)
        ELSE
                wert2$=wert2$+MID$(wert1$,t,1)
        ENDIF
NEXT
PRINT wert2$,10,50
RETURN wert2$
ENDFUNCTION // KONV

 

Offline Qedo

  • Mr. Polyvector
  • ***
  • Posts: 145
  • UNUS SED LEO
    • View Profile
Change KONV in KONV$ (2 places).
The second \ disappears because \ is a special character and works in tandem with the next

Offline dreamerman

  • Mr. Polyvector
  • ***
  • Posts: 214
    • View Profile
    • my personal website
I will just add a little to above response from Qedo.
In GlBasic there are 3 types of variables: # - floating point (default), % - integer, $ - string. You should always use those chars in declaration of vars and also functions, so in this code use KONV$ instead of KONV (that would return floating point value).
Second issue: GLB converts code to C++ sources, that are later compiled by GCC, in C++ (and other languages) the '\' is an escape character for special commands like regex expressions, and compiler tries to handle '\\' as such special command. It only aplies to statically typed string's so it wont appear in string from Input, grabbed from files, made with Chr$() and so on.
To avoid this just use:
GLOBAL a$="Test" + CHR$(92) + CHR$(92) + "//Test"
Check my source code editor for GLBasic - link

Offline uwe_gl

  • Mc. Print
  • *
  • Posts: 5
    • View Profile
Thanks,
with this help i chnage the code. It works now for me.
 :)