dkl
Full Member
Posts: 234
|
Post by dkl on Jul 30, 2020 21:50:44 GMT -5
I want to find a section within a Array$ after it has been read from a file and delete it. It will be a random length every time eg A$(1) is read from a file and reads as below A$(1)="Jo Bloggs (Fred Higgins) Micky Mouse (Minnie Mouse)". This Array$ can have up to 20/30 names! I want to delete all names within in the brackets and put "Jo Bloggs" and "Mickey Mouse" into a seperate Array$ so that I end up with B$(1)="Jo Bloggs " B$(2)="Micky Mouse" I been using left$(A$,x) to find where the name in brackets begins which works fine, but I'm stuck after that.
I've tried various methods using replstr$, but it only places it doesn't delete
I'm sure this is very easy, but cannot get it to work ALSO If I use Trim$(A$) to get rid of spaces at the beginning and end of A$ eg " Jo Bloggs " to "Jo Bloggs" I get an error. In the HELP file the example only uses print len(Trim$(A$)) and sure enough i get the LEN, but it doesn't explain further. Has the command altered in newer versions? I stil use LB V4.5.1
I hope I have made myself clear. Any help would be appreciated Many Thanks in advance
|
|
|
Post by Brandon Parker on Jul 30, 2020 22:41:11 GMT -5
Hello, and welcome to the forum!
See if this will get you started in the right direction. This assumes the string will always be formatted the same way and will always be a complete set of name pairs (one outside parenthesis followed by one inside parenthesis).
A$(0)=" Jo Bloggs (Fred Higgins) Micky Mouse (Minnie Mouse) "
Print "Starting String" Print A$(0) Print
'Removes leading & trailing white-space A$(0) = Trim$(A$(0))
Print "Trimmed String" Print A$(0) Print
While Instr(A$(0), "(") 'Grab the next name held within parenthesis and store it in B$() B$(nameCount) = AfterLast$(UpTo$(A$(0), ")"), "(") 'Remove that name held within parenthesis and store 'the result back into A$(0)for further processing A$(0) = Trim$(UpTo$(A$(0), "(");After$(A$(0), ") ")) 'Increment the nameCount variable to increase the working element 'for the B$() array nameCount = (nameCount + 1) Wend
Print "Resulting String held in A$(0)" Print A$(0) Print
Print "Resulting Strings held in B$(0) and B$(1)" Print B$(0) Print B$(1)
End
You should see the following output when running the code supplied above. (Note that the leading & trailing spaces of the initial A$(0) string may not show in the quote below...)
Feel free to ask further questions if you need ...
{:0)
Brandon Parker
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Jul 31, 2020 4:41:07 GMT -5
Many thanks, that looks like it's exactly what I need. I did come up with a very conveluted way to do it, but it was very clumsy and i got into a huge mess, it used for/next loops and checking for brackets etc......although it did work Thank you for your help, fast response and the comments really help
|
|
|
Post by Brandon Parker on Jul 31, 2020 9:20:49 GMT -5
No problem!!
We are always here to help ...
{:0)
Brandon Parker
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Aug 1, 2020 22:25:36 GMT -5
I learned 4 new Commands from your last post.......a great help! Perhaps you could be kind enough to help here too? I began my Basic programming back in the late 70's when half of the Commands in Liberty Basic didn't even exist, so I'm still not really up to speed with 21st Century Command!! Please forgive this mundane question! I have a row of buttons "A, B, C,.... to Z", which I can click and it will search my database beginning with the relevant alpha-numerical choice. As you can only '[goto/sub]' within the Command syntax and cannot assign info to anything (which would be nice in LB5!), at present I use the '[goto[' function to assign the 'button' value to the variable eg Hit button "A" - goto SearchValue$="A" then goto [Search] to look for required data
All very long winded with 26 Alpha-numeric and 10 numeric characters!!
There must be an easier way?
|
|
|
Post by tsh73 on Aug 2, 2020 2:18:26 GMT -5
Sure! You handle event with same SUB on all buttons - SUB gets button handle, like "#main.B" - you get part of the handle that interests you.
But you cannot goto [Search] from within sub - has to be converted to sub too (or do it just in this sub)
' Form created with the help of Freeform-J v.261006 nomainwin
WindowWidth = 376 WindowHeight = 410
UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2)
button #main.A, "A", buttonClick, UL, 26, 16, 48, 25 button #main.B, "B", buttonClick, UL, 70, 16, 48, 25 button #main.C, "C", buttonClick, UL, 118, 16, 48, 25 button #main.Z, "Z", buttonClick, UL, 254, 16, 56, 25 statictext #main.statictext5, "log", 22, 46, 144, 20 texteditor #main.log, 22, 66, 320, 275 menu #main, "Edit" '<--- Texteditor Menu can be moved but not removed.
open "Push the button" for window as #main print #main, "trapclose [quit.main]"
print #main, "font ms_sans_serif 10"
wait
[quit.main] Close #main END
sub buttonClick handle$ SearchValue$=right$(handle$, 1) #main.log, "SearchValue$=";SearchValue$ end sub
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Aug 2, 2020 2:55:53 GMT -5
Ah... That's a great help. Now I think I understand what the 'handle' is all about now. I see that by adjusting the value in SearchValue and also the use of right$, mid$, left$ that it returns different results. I never realised that! I presume it works like that for ALL buttons, radiobuttons, bmpbuttons, listboxes, etc? Many thanks
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Aug 2, 2020 4:02:51 GMT -5
But you cannot goto [Search]from within sub - has to be converted to sub too (or do it just in this sub)
I tried just doing the search in the sub - I'm using the old fashioned For/Next loop way and it would find 1 file and then it wouldn't work again, (checked with debug) - it worked before outside the sub, so I know its not my programming, just my understanding of programming! I'm used to the old way of gosub/return routine! I'm going to have to do a good read up on sub routines and search file handling and then get back to you. Perhaps you could suggest a good article, please.
In the meantime ............how do I get out of the sub? I've tried using exit sub, but I still sit there. the programme continues running I just can't Search till I sort it out. Perhaps I can use your commands a different way without the sub However, I've learned some more Command usage which is good:) Thank you
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Aug 2, 2020 7:42:14 GMT -5
I got the search routine running inside the sub, but every time I want to run it I have to load the file to be searched INSIDE the sub as anything outside of the subroutine isn't understood. It means ALL variables etc have to be relisted within the routine for it to run correctly. I am hoping I am correct here.Its what it said in the LB4 Help ebook!
I'm not sure I understand how to setup a specific sub routine just as a search module. I need to do more research into into search routines (excuse the pun!) I understand the Commands Word$, replstr$, Afterlast$, After$, Upto$. Are there others I specifically need when creating a search routine?
Thank you, I'm learning a lot!
|
|
|
Post by Brandon Parker on Aug 2, 2020 8:42:31 GMT -5
All you need to do to put your search routine inside a Subroutine is to either make the data it uses Global or pass that data into the subroutine (ByValue - default, or ByRef - requires the statement) ... look those up in the help file. Using ByRef, Liberty BASIC points to the data that is being passed in instead of making a copy and using that copy. This allows for manipulating data outside of the Subroutine's local scope. Or, you can use a function and return a value from the function (as well as using ByRef ...). There are many options available; it just depends on how you want to do it.
As far as commands you might need, just browse through them and look at the bottom of each description where there are links to similar/associated commands. You will eventually find everything you might need, or be able to craft something custom if it does not exist within LB.
{:0)
Brandon Parker
|
|
|
Post by tsh73 on Aug 2, 2020 13:49:01 GMT -5
dkl, you really should post some code If you post code we could look and find ONE reason for why
but now we could only guess. And to guess about unlimited ways it could be done wrong - well, pointless.
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Aug 2, 2020 23:20:25 GMT -5
It was just an incorrect variable!! I didn't realise immediately that any variables/array$ set OUTSIDE of the subroutine were not understood INSIDE the sub routine.
I'm going to work now on using Globals as Brandon suggested and see what I get. I'll try using some of the new Commands I've learned. The last search routine only involved for/next loops and the use of left$/mid$/right$ + if/then statements. It worked though. As did your code once I had worked it all out and added my search routine to it.
I all still puzzled though about how to exit from a sub statement. I often use sub routines to execute some code I want to use often and then want to return to the main program without having to wait for user input. Also I might use a sub routine to open a new GUI window and display some info before closing the GUI window and returning to the main programme. In this case, using an OK button would easily return me to where I left off within the programme if I use gosub/return. I'm not sure that would happen if I use Call/sub?
I added my search programme to the code you gave me and that all eventually all worked fine, but how do I get out of the sub routine to go elsewhere to print the result to the Main GUI?
As I said I like to use sub routines a lot to execute a module and then return to the main programme rather than jumping around all over the place using goto statements. (I know my way - and using sub routines - is often frowned upon, because its easy to get lost within the programme if you use too many sub routines! Which makes sense!
So..... all I'm asking in a long winded way is....................What is the best way to get out of the sub routine and how do I get out of the sub routine without using user into?
Many Thanks
|
|
|
Post by Chris Iverson on Aug 3, 2020 0:54:52 GMT -5
CALL/SUB, or invoking a FUNCTION, behave the same way as GOSUB. Execution returns to the point where the invocation was made. i.e. with GOSUB, execution continues with the line after the GOSUB command. With CALL, or a function call, it does the same thing.
This will happen when execution either reaches the end of the SUB/FUNCTION, or when it hits an EXIT SUB/FUNCTION statement, which behaves similarly to RETURN.
As for subroutines making things confusing, I find that if they're used well, they can actually make things EASIER to follow, by breaking different segments of execution up into logical components that can be simply looked over if not needed.
This allows you to keep the main logic of your program very clean and concise.
For example, if you had a program where you load a bunch of specially-formatted data from a file, and then work with that data, the main logic gets cluttered up with all of the file-handling routines, and it can be difficult to pick out where the file-handling ends and data processing begins at a glance.
If you have a subroutine that handles file loading, you can just call the subroutine, and the actual code in the main part of the program becomes very clean, and very close to the actual high-level logic:
1. Load data from file 2. Do work with data
The only reason you'd need to look at the file handling code is if you needed to change it. Otherwise, you can just set it aside.
|
|
|
Post by tsh73 on Aug 3, 2020 3:29:29 GMT -5
I would say in general you should not have WAIT in your sub I think these are likely responsible for "event works only first time" things
|
|
|
Post by Brandon Parker on Aug 3, 2020 16:14:19 GMT -5
I would say in general you should not have WAIT in your sub I think these are likely responsible for "event works only first time" things Also, you will likely blow the stack up eventually ... NEVER EVER ... wait inside a Subroutine/Function. {:0) Brandon Parker
|
|