Hi Jane
I can see you have coaxed a solution from Claude but really the code it has generated is pretty woeful.
I was really impressed with your perserverance telling it/him about the instring parameters and was also impressed with Claude’s debugging skills, adding in debugging information and getting you to run it and report back. Still the end result leaves a lot to be desired. I have heard John H. say he doesn’t bother looking at the generated code - as long as it works - and this does seem to be the direction with vibe programming but I do shudder thinking of all the, (shall we say) “less than ideal” code this will result in.
If we start at the first line of code in ExtractDate we see
TempText = CLIP(pText)
where tempText is String(255). So that clip does nothing apart from slow things down. And why bother with the assign at all - all it does is restrict the imput to 255 chars - what if the date is after that? I have railed for years against people using unnecessary clips and this code is just filled with them:
MonthStr = CLIP(SUB(pDateStr, 1, Slash1 - 1))
DayStr = CLIP(SUB(pDateStr, Slash1 + 1, Slash2 - Slash1 - 1))
YearStr = CLIP(SUB(pDateStr, Slash2 + 1, LEN(CLIP(pDateStr)) - Slash2))
those three fields are all strings so again clip doesn’t achieve anything. Then take this example where RESULT is string(50):
Result = CLIP(Tokens[i]) & ' ' & CLIP(NextToken) & ', ' & CLIP(YearToken)
RETURN Result
again a wasted clip - in order to return the clipped result it would be better to
return CLIP(Tokens[i]) & ' ' & CLIP(NextToken) & ', ' & CLIP(YearToken)
I’m sure AI will get better and will one day write reasonable Clarion code but it seems to be nowhere near it yet, at least in my experience.
My experience thus far is that it would be easier to write the code from scratch than waste my life correcting infuriating code! YMMV
Anyway seeing the original question mentioned StringTheory I thought I would knock up some code for comparison using ST. To me it is much cleaner and efficient. If you have time could you please test this and confirm it works as desired. The only difference really is I chose to return the Clarion date (as a long) or zero where no valid date is found, and was more flexible about the separators for the component parts of a date.
Anyway see how you go
cheers
Geoff R
ExtractDate PROCEDURE (STRING pText)
! Extracts dates in formats:
! mm/dd/yyyy mm-dd-yyyy
! Mar 5, 2024 or Mar 5 2024 or Mar 5th 2024
! March 21 2025 or March 21, 2025 or March 21st 2025
!
! Returns Clarion date
!
! (c) 13th October 2025, Geoffrey Robinson and released under MIT license
!
st stringTheory
x long,auto
month long
day long
year long
CODE
st.setValue(pText, st:clip)
st.splitIntoWords(,,,false) ! false=don't use smart words
loop x = 1 to st.records()
if ~month
month = st.getLine(x)
if month >= 1 and month <= 12
! valid numeric month
else
case lower(st.getLine(x)) ! check month names
of 'jan' orof 'january' ; month = 1
of 'feb' orof 'february' ; month = 2
of 'mar' orof 'march' ; month = 3
of 'apr' orof 'april' ; month = 4
of 'may' ; month = 5
of 'jun' orof 'june' ; month = 6
of 'jul' orof 'july' ; month = 7
of 'aug' orof 'august' ; month = 8
of 'sep' orof 'september' ; month = 9
of 'oct' orof 'october' ; month = 10
of 'nov' orof 'november' ; month = 11
of 'dec' orof 'december' ; month = 12
else ; month = 0 ! invalid month name
end
end
elsif ~day
st.setValueFromLine(x)
if st.isAllDigits()
day = st.getValue()
else
st.lower()
if st.endsWith('st') or st.endsWith('nd') or st.endsWith('rd') or st.endsWith('th')
st.adjustLength(-2)
day = st.getValue()
end
end
if day < 1 or day > 31 ! invalid day number
day = 0
month = 0
x -= 1 ! go back a token
end
elsif ~year
year = st.getLine(x)
if year < 1900 or year > 2100 ! some year validation
year = 0
month = 0
day = 0
x -= 2 ! go back two tokens
else
return date(month,day,year) ! date found
end
end
end
return 0 ! no valid date found
#Edit 1: tweaked code with invalid year to go back two tokens
#Edit 2: ditto for invalid day - go back one token