|
Post by jarychk on Jul 14, 2022 6:02:25 GMT
Here is a example program which removes the spaces and comma from a date, if written in some format as MMM DD, YYYY and reports it back as MMMDDYYYY.
Big difficulty finding how to make it work properly and also how not to make the program stop the rest of computer functioning. A couple times I had to use Task Manager to turn off Just BASIC.
Finally I cleaned all the problems and mistakes. I believe this code is less efficiently written than it could be written. I display it here both for any beginner's learning and for someone to possibly make more efficient code.
'rewording of descriptive remarks 'Condensing an alphanumeric form of a date to remove spaces and comma 'Uses two while loops; one for the spaces and one for the comma 'po is position being checked in the string. 'sz is current size of the string. 'shrink$ most obviously the string while going through checks and revision. ' 'Already understood that start string checking begins at position 4 because the alphabetic 'part of the string has the necessary three characters indicating the month name.
shrink$="" 'the condensed date of today
print "Today' date is using date$() ";date$() input "press enter when you are ready for condensed form of that."; dum$ LET shrink$=date$()
sz=len(date$())
print "String size for the date is ";sz
po=4 while po<=sz if mid$(shrink$,po,1)=" " then shrink$=left$(shrink$,po-1)+right$(shrink$,sz-po) sz=sz-1 end if po=po+1 wend
po=5 while po<=sz if mid$(shrink$,po,1)="," then shrink$=left$(shrink$,po-1)+right$(shrink$,sz-po) sz=sz-1 end if po=po+1 wend
print print "Made way through the condesning loop." print "The date as condensed or shrank is, ";shrink$ end
|
|
|
Post by jarychk on Jul 14, 2022 7:02:50 GMT
The same thing but now performing the date as string condensing in a function:
print "Today' date is using date$() ";date$() input "press enter when you are ready for condensed form of that."; dum$ LET shrink$=date$()
sz=len(date$())
print "String size for the date is ";sz
print print "Made way through the condensing loop." 'print "The date as condensed or shrank is, ";shrink$ print "The date as condensed or shrank is ";shrinkD$(shrink$, sz)
END
function shrinkD$(fecha$, sz) po=4 while po<=sz if mid$(fecha$,po,1)=" " then fecha$=left$(fecha$,po-1)+right$(fecha$,sz-po) sz=sz-1 end if po=po+1 wend '------------------------------------ po=5 while po<=sz if mid$(fecha$,po,1)="," then fecha$=left$(fecha$,po-1)+right$(fecha$,sz-po) sz=sz-1 end if po=po+1 wend ' shrinkD$=fecha$ end function
|
|
|
Post by tsh73 on Jul 14, 2022 8:30:31 GMT
Since things you do fits with "filtering characters" I'll post (what I think is) common way to filter characters
It goes like this
source is Old MacDonald had a farm, E-I-E-I-O!
Only letters from the list: >OlMaI < Ol Maal a a a IIO
Only letters by condition, say uppercase OMDEIEIO
Only letters NOT from the list: >OlMaI < dcDondhdfrm,E--E--!
Only letters NOT fulfilling condition, say NOT uppercase ld aconald had a farm, ----!
s$="Old MacDonald had a farm, E-I-E-I-O!"
print "source is" print s$ print
'passing only allowed characters, by list list$="OlMaI " 'space too print "Only letters from the list: >";list$;"<" '"," to show space is in res$="" for i = 1 to len(s$) c$=mid$(s$,i,1) if instr(list$, c$) > 0 then res$=res$+c$ end if next print res$ print
'passing only allowed characters, by condition print "Only letters by condition, say uppercase" res$="" for i = 1 to len(s$) c$=mid$(s$,i,1) if c$=upper$(c$) and lower$(c$)<>upper$(c$) then ' uppercase? res$=res$+c$ end if next print res$ print
'removing only allowed characters, by list list$="OlMaI " 'space too print "Only letters NOT from the list: >";list$;"<" '"," to show space is in res$="" for i = 1 to len(s$) c$=mid$(s$,i,1) if instr(list$, c$)=0 then res$=res$+c$ end if next print res$ print
'removing only allowed characters, by condition print "Only letters NOT fulfilling condition, say NOT uppercase" res$="" for i = 1 to len(s$) c$=mid$(s$,i,1) if not(c$=upper$(c$) and lower$(c$)<>upper$(c$)) then ' uppercase? res$=res$+c$ end if next print res$ print
|
|
|
Post by jarychk on Jul 14, 2022 9:03:11 GMT
Some of the code used in your list/filtering/OldMacDonaldHadAFarm example program, although too compact and complicated at first, is starting to give a sense of this filtering.
I am not ready to try it now, but if the list to compare characters is A through Z, AND a through z, AND 0,1,2,3,4,5,6,7,8,9; if all that were in this special list of acceptable characters, then someone should be able to use a FOR loop and remove any character from a chosen string which is not in the list.
I still do not understand the code process you showed. Maybe I will want to review it some more --- several times.
. . A few minutes after examining your code, I am beginning to understand the loop better. Not perfectly; just better.
|
|
|
Post by jarychk on Jul 14, 2022 9:12:47 GMT
tsh73,
Do I understand this properly? You have a reference string of all the acceptable characters. The user in some way has a string with some unwanted characters. Program loops through the string(which may contain unwanted characters) from left to right, and builds a new string using the acceptable characters.
|
|
|
Post by tsh73 on Jul 14, 2022 10:13:33 GMT
Yes program takes character into c$ one by one and desides if it want c$ in output string (res$) or not Condition
if instr(list$, c$)>0 then checks if c$ is in list$
EDIT of cource if you change condition to
if instr(list$, c$)=0 then you will get all c$ that is NOT in list$
|
|
|
Post by jarychk on Jul 14, 2022 18:34:28 GMT
Good. Then I understand this filtering much better now. I tried to make a new simple program and reproduce what your code is doing; but I did not make it into a function. A few of the variables are different. How you know ways to make such economical code is amazing.
' only example here. reference list and given string to filter will differ according to purpose of program.
rem month names will not contain Q q K k X x W w reflist$="ABCDEFGHIJLMNORSTUVYabcdefghijlmnoprstuvy0123456789"
givend$="July 14, 2022"
'condense this given date string
Q=len(givend$)
shrank$="" for i=1 to Q c$=mid$(givend$,i,1) if instr(reflist$,c$)>0 then shrank$=shrank$+c$ end if next i
PRINT shrank$;", the condensed form of given ";givend$
END
|
|
|
Post by xxgeek on Jul 14, 2022 19:38:02 GMT
Here are a couple of alternative ways to get same resulting date format. One using the name of the month, the other using the number of the month (month/day/year)
Hope they can help you or someone else in the future.
dim s$(13) for x = len(date$()) to 1 step -1 s$(x) = mid$(date$(),x, 1) if s$(x) = " " or s$(x) = "," then s$(x) = "" else s2$ = s$(x);s2$:s$=s2$ next x
print "newDateFormat = ";s$
call fixdate
sub fixdate fixDate$ = Date$("mm/dd/yyyy") 'set up the date format that works with a filename(remove the /) fix1$ = word$(fixDate$, 1, "/") fix2$ = word$(fixDate$, 2 ,"/") fix3$ = word$(fixDate$, 3 ,"/") fixeddate$ = fix1$;"-";fix2$;"-";fix3$ 'change to fixeddate = fix1$;fix2$;fix3$ to remove dashes from resullt print fixeddate$ end sub
|
|
|
Post by jarychk on Jul 14, 2022 22:38:45 GMT
xxgeek, I almost thought I was understanding your first sample of code, but then, .... I do not know what is the s2$ variable, so I am prevented from really understanding the IF THEN code line. The section sample code appears to be trying to replace any single space or comma with the empty-string; and then after that, I am confused.
I have not looked at the second sample of code yet in your posting.
|
|
|
Post by xxgeek on Jul 15, 2022 2:28:19 GMT
The s2$ is an undeclared variable. No need to declare it. When the code is run the string from date$() is checked character by character.If the character is a space or a comma s2$ becomes "" (basically no value so doesn't get added to s$, but all other characters get recorded to s2$ and added to s$[/div] Basically s2$ holds the character through the loop so it can be added to s$ In the following code I took s$ =s 2$ and put it on it's own line to make it more clear. Hope that helps.
dim s$(13) for x = len(date$()) to 1 step -1 s$(x) = mid$(date$(),x, 1) if s$(x) = " " or s$(x) = "," then s$(x) = "" else s2$ = s$(x);s2$ 'this line adds s2$ to s$ when a comma or a space is NOT detected s$ = s2$ next x
print "newDateFormat = ";s$
|
|
|
Post by jarychk on Jul 15, 2022 2:53:41 GMT
xxgeek, Nice Try, but I'm still confused; but don't worry about it. Maybe I will recheck your last code example later, a couple or so more times and I might figure how the code works.
Some times happen that I can figure how to do something, but the code is less efficient than somebody else would do. Some times I make less inefficient code for what I find how to do, but the code is still less efficient than somebody else would do.
|
|
|
Post by jarychk on Jul 15, 2022 3:02:28 GMT
The s2$ is an undeclared variable. No need to declare it. When the code is run the string from date$() is checked character by character.If the character is a space or a comma s2$ becomes "" (basically no value so doesn't get added to s$, but all other characters get recorded to s2$ and added to s$ [/div] Basically s2$ holds the character through the loop so it can be added to s$ In the following code I took s$ =s 2$ and put it on it's own line to make it more clear. Hope that helps.
dim s$(13) for x = len(date$()) to 1 step -1 s$(x) = mid$(date$(),x, 1) if s$(x) = " " or s$(x) = "," then s$(x) = "" else s2$ = s$(x);s2$ 'this line adds s2$ to s$ when a comma or a space is NOT detected s$ = s2$ next x
print "newDateFormat = ";s$
[/quote] Continued thoughts on this: At the very least, I know that your code checks one character at a time for single-space and comma; and while building the fresh new string through the loop, will build using the current character but not build with that character if is space or comma. I should be able to work with this and attempt to reproduce what your code does, without trying to directly copy your code. (EDIT: The quoting did not work correctly. No understand why)
|
|
|
Post by xxgeek on Jul 15, 2022 3:54:06 GMT
No wonder you're confused. I screwed that explanation royally. It's wrong. Disregard it completely.. Another attempt. You are confused only with the if then line so I'll explain it if I can (don't trust my explanations ) if s$(x) = " " or s$(x) = "," then s$(x) = "" else s2$ = s$(x);s2$ Can also be written
if s$(x) = " " or s$(x) = "," then s$(x) = "" else s2$ = s$(x)+s2$ 'might make it more clear using a plus sign instead of semicolon if s$(x) = " " or s$(x) = "," then s$(x) = ""
As it loops the value of s$(x) is first checked for a comma or space. If detected they are both set to nul values by s$(x) = "" else s2$ = s$(x)+s2$ Else if space or comma is NOT detected that value s$(x) gets added to the s2$ variable.
On first loop that value is "" or no value. So s2$ has no value to add to itself . Only s$(x) has a value to add to s$2
Next loop s2$ WILL have a value, and gets added to itself along with s$(x) - basically building the new date formatted string
|
|
|
Post by xxgeek on Jul 15, 2022 5:08:51 GMT
After staring at my own code I just made it a little more efficient. Removed s$=s2$ - unnecessary.
dim s$(12) for x = len(date$()) to 1 step -1 s$(x) = mid$(date$(),x, 1) if s$(x) = " " or s$(x) = "," then s$(x) = "" else s2$ = s$(x);s2$ next x
print s2$
|
|
|
Post by jarychk on Jul 16, 2022 0:29:13 GMT
xxgeekI tried to reproduce the process your code does without trying to simply copy it, but to use what I understand the basic idea was intended to do. It appears to still not be as efficiently written as yours. print "Another string condense example try." print "Example strings could be 'july 15, 2022' and march 10, 2021" print " Let us use non function first." print "To end the program, just enter the an empty string (meaning press" print "Enter key without typing in anything)." print [goagain]
print "Give us a date in the form of 'MONTH dd, yyyy', for which MONTH" print "is any spelling you wish for the month. " : input gd$ cls
IF gd$="" then END
REM gd$ 'string or date user gives as input, string which is to be shrunk LET shrd$="" 'the shrunken string being rebuilt LET ch$="" 'any character to add to the string being built
sz=len(gd$)
'HERE DO THE SHRINKING for i=1 to sz if mid$(gd$,i,1)=" " or mid$(gd$,i,1)="," then ch$="" else ch$=mid$(gd$,i,1) shrd$=shrd$+ch$ end if next i
PRINT "WELL NOW WE'VE the date of ";gd$;" shrunken down to ";shrd$ PRINT goto [goagain]
END
|
|