ForEach for Strings

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ForEach for Strings

Post by mk-soft »

Split string to list ... and more

Link: SplitString to list or array ...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: ForEach for Strings

Post 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.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: ForEach for Strings

Post by mk-soft »

-1

I think it's not basic style
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: ForEach for Strings

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: ForEach for Strings

Post 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.
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: ForEach for Strings

Post by Seymour Clufley »

mk-soft wrote: Fri Jul 19, 2024 8:11 amI think it's not basic style
So what?
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Rinzwind
Enthusiast
Enthusiast
Posts: 679
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: ForEach for String

Post 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.
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: ForEach for String

Post 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 :)
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: ForEach for Strings

Post 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"
Post Reply