help converting function from vb6 to gambas
Posted
#1
(In Topic #419)
Trainee
Can someone help me convert a function a wrote several years ago from VB6 to Gambas3?
'Random letters in a string
Public Function X(ByVal sdata As String)
Dim OldWord As String
Dim NewWord As String
Dim temp As String
OldWord = sdata
While Not OldWord = vbNullString
Dim RandomPosition As Integer = StrRnd(Len(OldWord))
Dim RandomLetter As String = Mid(OldWord, RandomPosition, 1)
NewWord = NewWord + RandomLetter
For i = 1 To RandomPosition - 1
temp = temp + Mid(OldWord, i, 1)
Next
For j = RandomPosition + 1 To Len(OldWord)
temp = temp + Mid(OldWord, j, 1)
Next
OldWord = temp
temp = Nothing
End While
If Not NewWord = sdata Then
X = NewWord
Else
X(NewWord)
End If
End Function
Function StrRnd(ByVal i)
Dim x As Integer
Randomize()
StrRnd = Int((Val(i) * Rnd()) + 1)
End Function
Posted
Enthusiast

http://wordpress.gambas.one/
several good starter books
If you have an ereader … this is a good book
https://www.amazon.com/Beginners-Guide-Gambas-Revised-Rittinghouse-ebook/dp/B006C75I66/ref=sr_1_fkmr0_1?dchild=1&keywords=A+Beginner%27s+Guide+to+Gambas+-+Revised+for+Version+3+--+.PDF&qid=1590439795&sr=8-1-fkmr0
Posted
Trainee
Posted
Enthusiast

<IMG src="https://i.imgur.com/FABVT6O.png">
</IMG>
Posted
Regular

I played around with your functions and i got them to work. I had to take some liberties to change things around a bit. For example, "Temp" is already a built-in function in Gambas. And "X" for the name of a function is a bit too general, in my opinion. Anyways, on to the code:
Code (gambas)
A snafu I noticed in your original code is that the first function appears to call itself again, from within itself, if "NewWord" is blank. I changed this to be handled by putting the call to the first function into a "Do" loop like this:
Also, the integer "x" in the second function doesn't appear to be used for anything. In any case, the results I got from calling the first function 5 times are:
Code
sba mcgaba isis
masabaibc gss i
aba sbssmiai gc
s abiabmsiasgc
baasbsi siamgcI hope I didn't change things so much that this output isn't what you were expecting. I hope this helps you.
Posted
Trainee
I will check your code and analyze differences. I also got the book grayghost recommended me and i will start reading it.
PS1: Sorry for any mistakes , english is not my native language.
PS2: I had some troubles installing gambas3 on ubuntu but i figured it out , if it helps someone on basic ubuntu 20.04 it fails to start because it needs "gambas3-gb-form-print" ; a simple sudo apt install gambas3-gb-form-print will fix any problem
Posted
Regular

PS2: I had some troubles installing gambas3 on ubuntu but i figured it out , if it helps someone on basic ubuntu 20.04 it fails to start because it needs "gambas3-gb-form-print" ; a simple sudo apt install gambas3-gb-form-print will fix any problem
If you used Discover to install Gambas, your missing half of the Gambas libraries (including gambas3-gb-form-print). I found this out when installing Gambas for the first time on Kubuntu. To do a complete install use Synaptic or from the command line use sudo apt install gambas3. It will save you a lot of head aches in the future.
Posted
Trainee
PS:
Im playing with text files. I want to write a simple app that reads a .srt (subtitle file) then writes the same file with UTF8 encoding. For example i want to read the "invisible.srt" wich has character encoding WESTERN (ISO-8859-15) and write invisible2.srt with UTF8.
I know i can use Conv$ to convert from one encoding to another but..is there any way or function to convert from ANY encoding to UFT8? My app will need to eventualy convert all .srt files (wich may have different encodings) from one folder to UFT8
Or is there any GetEncoding posibility?
Dim xFile As File
Dim sLine As String
Dim yFile As File
xFile = Open Application.Path & "/" & "invisible.srt" For Input
yFile = Open Application.Path & "/" & "invisible2.srt" For Write Create
While Not Eof(xFile)
Line Input #xFile, sLine
Write #yFile, sLine & gb.NewLine
Wend
Close #xFile
Close #yFile
Posted
Guru

Consider the following code: -
Code (gambas)
- sChar = Replace(sChar, gb.NewLine, "") 'Get rid of the new line character 'uchardet' returns at the end of the line
Note that this is unnecessary xFile = Open Application.Path & "/" & "invisible.srt" For Input
Use &/ it works out if you need (or don't) a '/'
Both lines below will work: -
Open Application.Path &/ "invisible.srt" For Input
Open Application.Path &/ "/invisible.srt" For Input
Posted
Regular

Posted
Trainee
1 guest and 0 members have just viewed this.


