|
Post by metro on Dec 9, 2019 9:16:03 GMT -5
I was creating SQLite field names for a new table and because currency in the 1000,s is used I decided to use a semicolon for a separator BUT as the SQL statement requires the names to be quoted (') I thought it would be easy to just use REPLSTR$ to swap out the ; for ';' NOPE , bug I think Header$="ASXcode;Code;Underlying;Exchange;Type;Strike;Stop;ReqNum;CurSharePrice;Gearing;MiniValue;Perc2Stop"
============================= 'Endless Processing w$="';'" Header$ = REPLSTR$( Header$, ";",w$) 'Header$ = REPLSTR$( Header$, ";","';'") ======================================= 'THIS WORKS 'Header$=strRep$(Header$,";","';'") print Header$
wait
FUNCTION strRep$(str$,rep$,with$) ln = len(rep$) ln1 = ln - 1 i = 1
WHILE i <= len(str$) IF mid$(str$,i,ln) = rep$ THEN strRep$ = strRep$ + with$ i = i + ln1 else strRep$ = strRep$ + mid$(str$,i,1) END IF i = i + 1 WEND END FUNCTION
|
|
cundo
Full Member
Muchas Gracias!!
Posts: 146
|
Post by cundo on Dec 9, 2019 9:23:53 GMT -5
Hi metro. "W$" is a string in itself. What about using the CHR$( versions of these characters?
|
|
|
Post by metro on Dec 9, 2019 9:29:19 GMT -5
Hi metro. "W$" is a string in itself. What about using the CHR$( versions of these characters? Thanks cundo
oops ....my bad , the double quotes should not have been there w$ = "';'"
I had copied the code before I made the change and then posted forgetting to re-copy
|
|
|
Post by sarossell on Dec 9, 2019 10:09:19 GMT -5
Weird. I was able to get it to work by using an intermediary step.
Header$="ASXcode;Code;Underlying;Exchange;Type;Strike;Stop;ReqNum;CurSharePrice;Gearing;MiniValue;Perc2Stop"
w$="';'"
Header$ = REPLSTR$(Header$,";","''") print Header$
Header$ = REPLSTR$(Header$,"''",w$) print Header$
wait
end
Or this...
Header$="ASXcode;Code;Underlying;Exchange;Type;Strike;Stop;ReqNum;CurSharePrice;Gearing;MiniValue;Perc2Stop"
a$ = ";" : b$ = "''" : w$ = "';'"
Header$ = REPLSTR$(REPLSTR$(Header$,a$,b$),b$,w$) print Header$
wait end
|
|
|
Post by Carl Gundel on Dec 9, 2019 10:27:50 GMT -5
I was creating SQLite field names for a new table and because currency in the 1000,s is used I decided to use a semicolon for a separator BUT as the SQL statement requires the names to be quoted (') I thought it would be easy to just use REPLSTR$ to swap out the ; for ';' NOPE , bug I think Interesting. Thanks for the bug report.
|
|
bart
New Member
Posts: 7
|
Post by bart on Feb 26, 2020 7:43:08 GMT -5
G'day metro,
I came across this issue before myself. It's nothing to do with the characters ;' etc. Even if you do the following Liberty will go into an infinite loop:
a$="no" b$=replstr$(a$, "o", "oo")
Just need to watch what chars you're replacing and if need be, do as sarossell said and use 2 steps.
Nice mugshot btw!
|
|
|
Post by metro on Feb 26, 2020 8:21:34 GMT -5
G'day metro, I came across this issue before myself. It's nothing to do with the characters ;' etc. Even if you do the following Liberty will go into an infinite loop: a$="no" b$=replstr$(a$, "o", "oo") Just need to watch what chars you're replacing and if need be, do as sarossell said and use 2 steps. Nice mugshot btw! Thanks bart, I have gone back to using this function below
believe it or not I get about 10 roo's a night on my lawn coz it's so dry around here and my grass is green there are two brothers as big as the one in my mug shot, they share the does. on more than one occasion they have frightened the Cr#p out of me at night if i go to my shed No we don't have em throughout our cities, I live on acreage.
|
|
|
Post by sarossell on Feb 26, 2020 16:03:17 GMT -5
Metro,
My grand-daughter was captivated by your strRep$ function. (Is that a weird thing to say or what?!)
First, some background. For about the past six months, I've been watching over my 12 year old grand-daughter (long story). I live in Yuma, Arizona. Yuma is in the middle of the desert, very close to the Mexican border. Population: who gives a crap. Half the time, this town is full of migrant workers digging up lettuce in the Summer and the other half it's inundated with 80 year old Canadians trying to get away from the snow during the Winter. You can count the number of things a 12 year old can do here on one hand missing three fingers; the library and this weird trampoline place up the road. Oh, and maybe bowling. Okay, two missing fingers. And we have a couple of decent movie theaters. Alright, shuddup.
Anyway, she asked me one day what I was doing, since I seemed very intent at typing something on my laptop. I was writing some code in Liberty BASIC. Just goofin' around really. But for some reason, she was intrigued. So, I set up Liberty BASIC on another computer just for her, showed her the help file with all the commands and gave her little programming challenges to keep her busy. That was just around New Years. Since then, we have had very interesting conversations about BASIC, legacy computers, programming styles, electronics, compression, encryption, etc. Along the way, I accidentally taught her a lot of bad programming habits and then tried to teach her more conventional methods later.
One of the bad habits I imparted to her was based on my past with Sinclair BASIC. When I'm brainstorm-coding, I often use all caps, single letter variables, and very little space between commands. I did these things because, back in the day memory was at a premium. That's not the case today of course, but it's just been imprinted on my psyche.
So today, the subject came up about bugs that can sometimes happen in the language commands themselves. I showed her how the replstr$ command can get stuck in a loop if the replaced string contains the string being replaced. We had discussed bugs in code before of course, but she was just amazed that the language itself could also have bugs. She hadn't realized that Liberty BASIC was a program too.
When I showed her your function to replace the command, her eyes just lit up. She had no idea that you could create your own "commands" using the FUNCTION command. Then she surprised the heck out of me. She rewrote your function using every horrible bad habit I had shown her. I was both proud and mortified at the same time. Just look at this evil garbage...
FUNCTION REP$(S$,O$,N$) FOR N=1 TO LEN(S$) IF MID$(S$,N,LEN(O$))=O$ THEN R$=R$+N$ N=N+LEN(O$)-1 ELSE R$=R$+MID$(S$,N,1) END IF NEXT REP$=R$ END FUNCTION
I took one look at it and realized I had to fix her tiny little brain right away. But then I noticed she had replaced your WHILE..WEND loop with a FOR..NEXT loop. Huh? I didn't teach her that. She just intuitively assumed that she could run a FOR loop and adjust its variable value within the loop and it would just naturally know where to pick up from there. I mean, *I* knew that, but how did she figure that out? 'Cuz clearly, I had done a horrible job teaching her in the first place.
We had a lengthy discussion about this and then she asked me why couldn't you use the original buggy command and just make sure that the text you replace doesn't match the original. As I began to explain to her that that was what I had done in the past, she banged out this code:
function strRep$(s$,o$,n$) t$ = chr$(0) strRep$ = replstr$(replstr$(s$,o$,t$),t$,n$) end function
She was still using single-letter variables, but at least she used some structure and laid off the all-caps. More to the point, she had created a function that was exactly as I had imagined myself. Except she chose to use a character that would never need to be replaced "chr$(0)" as the intermediary string! WHAT?!
Acorn doesn't fall too far from the tree, I guess. I was so proud.
But then I though, that littler twerp! I've been programming in BASIC since 1981! And she caught up to me in less than two months?! So I smacked her on the head and ordered a pizza. Little jerk.
:@)
|
|
|
Post by Brandon Parker on Feb 26, 2020 23:49:55 GMT -5
I was creating SQLite field names for a new table and because currency in the 1000,s is used I decided to use a semicolon for a separator BUT as the SQL statement requires the names to be quoted (') I thought it would be easy to just use REPLSTR$ to swap out the ; for ';' NOPE , bug I think Interesting. Thanks for the bug report. Carl, Could this be that LB is replacing the string the first time, but not incrementing the position correctly based on the replacement string? I mean, if you look at the string "Hello; World!" and want to replace the ";" with "';'" you would find ";" at position 6 (foundPosition), but after replacing it with "';'" you would need to start back up at the "foundPosition + Len(replacementString$)". If the routine simply restarted at the next position after the foundPosition (i.e. 7), then it would find the ";" again at position 7 ... rinse and repeat until it crashes. That's just my thoughts on it since it seems to fail any time the second character in the replacement string is the same as the string being replaced. The same behavior is present when replacing ";" with "a;a" in my testing. Memory and Commit Size are also growing pretty steadily which, to me, points to the string continuously growing while LB attempts the find/replace operation. {:0) Brandon Parker
|
|
|
Post by metro on Feb 27, 2020 0:14:56 GMT -5
Metro, My grand-daughter was captivated by your strRep$ function. (Is that a weird thing to say or what?!) When I showed her your function to replace the command, her eyes just lit up. She had no idea that you could create your own "commands" using the FUNCTION command. Then she surprised the heck out of me. She rewrote your function using every horrible bad habit I had shown her. I was both proud and mortified at the same time. We had a lengthy discussion about this and then she asked me why couldn't you use the original buggy command and just make sure that the text you replace doesn't match the original. As I began to explain to her that that was what I had done in the past, she banged out this code: But then I though, that littler twerp! I've been programming in BASIC since 1981! And she caught up to me in less than two months?! So I smacked her on the head and ordered a pizza. Little jerk. :@) I'm more impressed she could get a word in! (Please don't take offense, sarcasm is a national sport down here)
Also Thank Meerkat for the function I stole it from Dan years ago
|
|
|
Post by sarossell on Feb 27, 2020 0:37:31 GMT -5
Ha! Rude! LOL.
Actually, she's surprisingly loquacious. I'm primarily textually verbose.
:@p
|
|
|
Post by meerkat on Feb 27, 2020 5:33:33 GMT -5
Ya! And I stole it from sarossell's grand-daughter..
|
|
|
Post by sarossell on Feb 27, 2020 14:06:23 GMT -5
Tough crowd!
|
|