Re: ForEach for Strings
Posted: Thu Jul 18, 2024 10:22 am
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
MyString$ = "1|2|3|4|5|..."
ForField temp$ in MyString$ Delimiter "|"
; do stuff with Temp$
NextField
Code: Select all
Dim myarray.i(10)
; Output all 11 entries
ForEach myarray()
Debug myarray()
Next
Code: Select all
Dim myarray.s(0)
; Redim myarray to 6 elements and assign the values, separated by comma, to the elements of the array
myarray() = SplitString("a,b,c,d,e,f", ",")
Code: Select all
ForEach SplitString("a,b,c,d,e,f", ",") As myarray()
Debug myarray()
Next
Whenever syntax updates come up, you and some others always bring up this argument, and I'm trying to understand it. What exactly makes syntax classify as "BASIC-style" in your head? IMO sometimes things need ditched to move onto better things, even portions of BASIC (line numbers)? For me, I personally don't care about syntactical rules set by the designers of the first BASIC 50 years ago and would rather just be able to loop over my strings
I like this, but I think the reason you can't return arrays from procedures is because it could create a dangling reference/memory leak very easily.NicTheQuick wrote: Fri Jul 19, 2024 2:17 pm I think it would be a more Basic approach when we do it in two steps.Of course this is only an idea.
- First allow iterating over a normal array:
Code: Select all
Dim myarray.i(10) ; Output all 11 entries ForEach myarray() Debug myarray() Next
- Then allow the implicit creation of arrays from functions:
Code: Select all
Dim myarray.s(0) ; Redim myarray to 6 elements and assign the values, separated by comma, to the elements of the array myarray() = SplitString("a,b,c,d,e,f", ",")
- Combine both features:
Code: Select all
ForEach SplitString("a,b,c,d,e,f", ",") As myarray() Debug myarray() Next
So what?
Agreed fully. The bug fixing spree was a welcome surprise so maybe Fred will surprise us, but sadly I'm not hopeful, PB's syntax is likely to stay what it is. I would very much like to see much expanded inline-C support at the very least though, so we can sort of use PB like BCX Basic and just put C code directly anywhere in our codeRinzwind wrote: Sat Jul 20, 2024 4:03 pm Anyway, lots of quality of life improvements for the programmer are possible. Probably to do with the "basic" asm compiler backend helping to keep it maintainable for one person. I hope after the welcomed bug fixing lately PB gets some actual syntax/language enhancements/modernization. But probably not, based on past feature requests. Just being realistic. In some cases even plain old boring c has nicer syntax (array declaration and initialization, structure return from function). Take a look at modern small procedural indy languages for ideas.
Code: Select all
Structure StringFieldMapType
ID.i
*pos.Character
string.s
delimiter.c
element.s
EndStructure
Global NewMap StringFieldMap.StringFieldMapType()
Procedure ResetStringField(string.s,delimiter.s)
With StringFieldMap(string)
\ID=0
\string=string
\pos=@\string
\delimiter=Asc(delimiter)
\element=""
EndWith
EndProcedure
Procedure NextStringField(string.s)
Protected *scan.Character
Protected cont
With StringFieldMap(string)
*scan=\pos
Repeat
Select *scan\c
Case 0
Break
Case \delimiter
cont=#True
Break
EndSelect
*scan+SizeOf(Character)
ForEver
If \pos=*scan
\element=""
ProcedureReturn #Null
Else
\element=PeekS(\pos,(*scan-\pos)/SizeOf(Character))
\pos=*scan+cont*SizeOf(Character)
\ID+1
ProcedureReturn #True
EndIf
EndWith
EndProcedure
Procedure.s StringFieldElement(string.s)
ProcedureReturn StringFieldMap(string)\element
EndProcedure
Procedure StringFieldId(string.s)
ProcedureReturn StringFieldMap(string)\ID
EndProcedure
s.s="1|2|3|4|five|sei|sieben|otto|neuf|deka"
ResetStringField(s,"|")
While NextStringField(s)
Debug Str(StringfieldId(s))+": "+StringFieldElement(s)
Wend
Debug "done"