Rasmus Lerdorf wrote:

On Sun, 31 Oct 2004, moshe doron wrote:

where the problem about 'taking it ever further'? I'm +1 for {-1}
and more +1 for {-4,2} python likeness.


That starts getting confusing.

Having my python hat on...

(some of this is just general comment after reading too much of this thread)

Yeah, if that actually worked that way in python, it would be confusing.
 You cannot do:

a="asdfghjkasdfghjk"
a[-4:2]

You get an empty string.  That is because, as you said, it has to be
start:end.

a[2:-4] works and the result is of course "dfghjkasdf".

You also cannot write to this:

a[2:4]="asdf"

That will throw an exception, as it should for apparent reasons.

Negative index in strings and arrays are extremely usefull, and I use
them a lot, but for some reason can never think why I use them.  Thats
probably because it's not tied to any particular type of operation.

There also is this which is very useful for the reason that you do not have to calcuate length in your script (in the second two examples):

a[:2] (grab the first two chars)
a[2:] (everything after the first two chars)
a[-2:] (grab the last two chars)

For python, it's a great construct both for readability and performance. PHP would only benefit from the ability to use this. However, it should be of course, not be in a micro release. It is also unlikely many people will use it since it would not be backwards compatible code.

Shane

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to