Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Future Improvements (https://forums.graalonline.com/forums/forumdisplay.php?f=10)
-   -   Array Slicing (https://forums.graalonline.com/forums/showthread.php?t=134256867)

coreys 11-09-2009 06:02 PM

Array Slicing
 
This is probably a bit of a large request because it involves the syntax of the language itself, but having slicing syntax similar to that of Python would be really useful.
For example:
PHP Code:

foo "bar";
baz foo[0:1]; // return a slice from index 0 to 1
echo(baz);      // will echo "ba"
baz foo[:-1]; // return a slice from the end
echo(baz;       // will echo "r" 

It's extremely useful and shortens common string and array operations greatly.
Really, better support for treating strings as arrays in general would be nice.

fowlplay4 11-09-2009 06:30 PM

what's wrong with using substring and subarray?

I guess this is just to make code shorter.

LoneAngelIbesu 11-09-2009 10:18 PM

Yeah. This just looks like an alternative substring() syntax...

coreys 11-09-2009 10:55 PM

It's not different, my point is it's cleaner and shorter. Which is important.

LoneAngelIbesu 11-10-2009 03:20 AM

Quote:

Originally Posted by coreys (Post 1537515)
It's not different, my point is it's cleaner and shorter. Which is important.

But it already exists. Let's not make GS2 even harder to learn by adding redundant syntax...

WhiteDragon 11-10-2009 03:56 AM

Hey look, it's another "this other language does this better so let's put it in GS2" thread!

coreys 11-10-2009 04:22 AM

Quote:

Originally Posted by LoneAngelIbesu (Post 1537568)
But it already exists. Let's not make GS2 even harder to learn by adding redundant syntax...

Make it harder? This would make it easier, and clearer. x.x

Tigairius 11-10-2009 04:25 AM

Personally, I just wish GS2's substring supported something like string.substring(-1) which would take off 1 character from the back of the string, rather than having to write out: string.substring(0, string.length() - 1)

coreys 11-10-2009 04:35 AM

That alone would be a very nice addition.

cbk1994 11-10-2009 05:20 AM

Quote:

Originally Posted by Tigairius (Post 1537588)
Personally, I just wish GS2's substring supported something like string.substring(-1) which would take off 1 character from the back of the string, rather than having to write out: string.substring(0, string.length() - 1)

Amen. I was trying to do something similar the other day and was surprised to find it didn't work.

LoneAngelIbesu 11-11-2009 01:49 AM

Quote:

Originally Posted by coreys (Post 1537583)
Make it harder? This would make it easier, and clearer. x.x

And redundant. A new scripter would be wondering, "What is the correct syntax? Do they do different things? Why are there two ways to do this? Am I doing it wrong if I do it this way?" etc.

Different languages have different ways of doing things. Python has this syntax. PHP has the array_slice() and substr() functions. GS2 has .subarray() and .substring().

Quote:

Originally Posted by Tigairius (Post 1537588)
Personally, I just wish GS2's substring supported something like string.substring(-1) which would take off 1 character from the back of the string, rather than having to write out: string.substring(0, string.length() - 1)

That seems like it wouldn't be terribly difficult to do.

coreys 11-11-2009 05:02 AM

You have a point, although I think it wouldn't be quite that much of a problem for new scripters. In any case, what Tig said I think is a pretty reasonable suggestion, and not hard to add in.

Inverness 11-11-2009 05:33 AM

Quote:

Originally Posted by coreys (Post 1537803)
You have a point, although I think it wouldn't be quite that much of a problem for new scripters.

Quote:

Originally Posted by The Zen of Python
There should be one-- and preferably only one --obvious way to do it.

You insult The Zen by attempting to get a feature added that already exists in another form. Not only that, but Python slicing uses start and end indexes, not start and length. That will simply add more confusion.

Admins 11-11-2009 03:02 PM

In Graal script we tried to prevent any confusion. Operators only have one meaning, that's why there is @ and + for strings and numbers, instead of using + for both. The array operator should also stay like it is, the tiles[x,y] syntax can already be confusing enough.
The substring(-1) extension could be interesting, although I'm not sure if it would break some scripts that accidently go below 0 for the index?

coreys 11-11-2009 03:44 PM

Well, I personally think if their script is accidentally searching for an index below 0 that it's their own fault anyways, but in that case you could use substring(0,-1), or something like that.


All times are GMT +2. The time now is 04:12 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.