Direct Substrings allocation

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Direct Substrings allocation

Post by charvista »

Actually, we have Left(), Right() and Mid(), and they work very well.

However, I would suggest direct substrings allocation:

Code: Select all

Define.s S="abcdefghijklmnopqrstuvwxyz"

Debug S[1,2] => Left(S,2) => "ab"
Debug S[-4] => Right(S,4) => "wxyz"
Debug S[1,5] => Mid(S,1,5) => "abcde"
Debug S[14] => Mid(S,14) => "nopqrstuvwxyz"

And that would accomplish something else very easily:
S[5,4]="EFGH" => "abcdEFGHijklmnopqrstuvwxyz"
That would also enhance the clarity.
Brackets used instead of parentheses to avoid confusion with arrays.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Direct Substrings allocation

Post by STARGÅTE »

charvista wrote:Brackets used instead of parentheses to avoid confusion with arrays.
-1

[n] is for static array like:

Code: Select all

Structure Example
	StructureUnion
		String.s{36}
		Month.s{3}[12]
	EndStructureUnion
EndStructure

Define Example.Example\String = "JanFebMarAprMayJunJuiAugSepOctNovDec"

Debug Example\Month[0]
Debug Example\Month[3]
Debug Example\Month[11]
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
charvista
Addict
Addict
Posts: 949
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Direct Substrings allocation

Post by charvista »

A pity.
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Direct Substrings allocation

Post by Danilo »

STARGÅTE wrote:-1

[n] is for static array like:

Code: Select all

Structure Example
	StructureUnion
		String.s{36}
		Month.s{3}[12]
	EndStructureUnion
EndStructure

Define Example.Example\String = "JanFebMarAprMayJunJuiAugSepOctNovDec"

Debug Example\Month[0]
Debug Example\Month[3]
Debug Example\Month[11]
charvista was asking for using brackets [ ] for PB standard type .s, so it would not affect your code.

Code: Select all

Debug Example\String[0,3] ; would return "Jan"
It is possible to do what she requested in other languages by overloading operators [].

The difference is where it is used, If used on the left hand side, it is a target of an expression.
If used on the right hand side, it is an expression like Mid().

Code: Select all

s.s = Example\String[0,3] ; would return "Jan"
Example\String[0,3] = "XYZ" ; would replace "Jan" with "XYZ"
In your seconds example:

Code: Select all

s.s = Example\Month[1][0,10] ; would return "Feb"
Example\Month[1][0,3] = "XYZ" ; would replace "Feb" with "XYZ"
Good idea. Maybe just too advanced and complicated for PureBasic.
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: Direct Substrings allocation

Post by moogle »

Danilo wrote:Good idea. Maybe just too advanced and complicated for PureBasic.
Most likely, I think I requested something like this before or at least mentioned it somewhere. It's a great thing about Python (where I learnt it from).

Code: Select all

Debug Example\String[3,] ; would return "FebMarAprMayJunJuiAugSepOctNovDec"
Stuff like that would help avoid having to use Len() , Mid(), Right() and Left()
Image
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Direct Substrings allocation

Post by Danilo »

moogle wrote:

Code: Select all

Debug Example\String[3,] ; would return "FebMarAprMayJunJuiAugSepOctNovDec"
Stuff like that would help avoid having to use Len() , Mid(), Right() and Left()
Agreed.

Maybe it would be better to use parenthesis for this feature in PureBasic? So strings are seen like array of characters
and it is not mixed with the brackets like STARGÅTE's example.
So if a string has parenthesis at the end, it is turned into functions Right, Left, Mid, Insert, Replace.

Code: Select all

Define.s s="abcdefghijklmnopqrstuvwxyz"

Debug s(1,2) ; => Left(s,2)  => "ab"
Debug s(-4)  ; => Right(s,4) => "wxyz"
Debug s(1,5) ; => Mid(s,1,5) => "abcde"
Debug s(14)  ; => Mid(s,14)  => "nopqrstuvwxyz"

s(5,4) = "EFGH" ; => "abcdEFGHijklmnopqrstuvwxyz"



Structure Example
   StructureUnion
      String.s{36}
      Month.s{3}[12]
   EndStructureUnion
EndStructure

Define Example.Example\String = "JanFebMarAprMayJunJuiAugSepOctNovDec"

Debug Example\Month[0]
Debug Example\Month[3]
Debug Example\Month[11]

Debug Example\String(1,3) ; would return "Jan"

Example\String(1,3) = "XYZ" ; would replace "Jan" with "XYZ"

Debug Example\Month[1](1,10) ; would return "Feb"

Example\Month[1](1,3) = "XYZ" ; would replace "Feb" with "XYZ"
:?:
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: Direct Substrings allocation

Post by moogle »

Danilo wrote:Agreed.

Maybe it would be better to use parenthesis for this feature in PureBasic? So strings are seen like array of characters
and it is not mixed with the brackets like STARGÅTE's example.
So if a string has parenthesis at the end, it is turned into functions Right, Left, Mid, Insert, Replace.
Looks like a good way to keep code compatible and have the new features.
Image
Post Reply