Page 1 of 1

Direct Substrings allocation

Posted: Sat Mar 23, 2013 7:47 am
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.

Re: Direct Substrings allocation

Posted: Sat Mar 23, 2013 10:01 am
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]

Re: Direct Substrings allocation

Posted: Sat Mar 23, 2013 11:43 am
by charvista
A pity.

Re: Direct Substrings allocation

Posted: Sat Mar 23, 2013 12:05 pm
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.

Re: Direct Substrings allocation

Posted: Sat Mar 23, 2013 3:24 pm
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()

Re: Direct Substrings allocation

Posted: Sat Mar 23, 2013 11:11 pm
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"
:?:

Re: Direct Substrings allocation

Posted: Sat Mar 23, 2013 11:49 pm
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.