Page 2 of 2

Re: ForEach for Strings

Posted: Thu Jul 18, 2024 10:22 am
by mk-soft
Split string to list ... and more

Link: SplitString to list or array ...

Re: ForEach for Strings

Posted: Fri Jul 19, 2024 6:16 am
by Seymour Clufley
+1

I think this syntax would be cool:

Code: Select all

MyString$ = "1|2|3|4|5|..."
ForField temp$ in MyString$ Delimiter "|"
  ; do stuff with Temp$
NextField
where Delimiter is a bolded keyword, like Step in the standard For..Next loop.

Re: ForEach for Strings

Posted: Fri Jul 19, 2024 8:11 am
by mk-soft
-1

I think it's not basic style

Re: ForEach for Strings

Posted: Fri Jul 19, 2024 2:17 pm
by NicTheQuick
I think it would be a more Basic approach when we do it in two steps.
  • 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
Of course this is only an idea.

Re: ForEach for Strings

Posted: Fri Jul 19, 2024 3:37 pm
by Quin
mk-soft wrote: Fri Jul 19, 2024 8:11 am -1

I think it's not basic style
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 :mrgreen: but to each their own, I am quite curious
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.
  • 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
Of course this is only an idea.
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.

Re: ForEach for Strings

Posted: Sat Jul 20, 2024 3:27 pm
by Seymour Clufley
mk-soft wrote: Fri Jul 19, 2024 8:11 amI think it's not basic style
So what?

Re: ForEach for String

Posted: Sat Jul 20, 2024 4:03 pm
by Rinzwind
PureBasic does not look like 80's microcomputer Basic. Does also not look much like QuickBasic or Visual Basic. There is not one Basic for better or worse. So maybe meant does not look like PureBasic?

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.

Re: ForEach for String

Posted: Sat Jul 20, 2024 5:12 pm
by Quin
Rinzwind 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.
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 code :)

Re: ForEach for Strings

Posted: Sat Jul 20, 2024 7:55 pm
by Michael Vogel
Just for fun - something completely different...
...yes, it's a simplified approach (single character delimiter) and nobody has asked for this here - anyhow this thread tends to collect heterogeneous opinions :P

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"