The idea was originally proposed by Marc Easen who created a patch and asked for help with putting together an RFC. I have yet to see a formal proposal but on the list Easen modified his idea so that it should apply to strings alone. With that in mind, would it really cause problems to have code like this:
$string = "Roses are red";
$string[-3] = "R"; // modifying $string[10]
echo $string; // Roses are Red
The negative indexing provides a pleasurable way to easily access part of a string; it's simple and should be fast, too.
I think Easen's proposal is laudatory and I for one would like to see it happen for PHP and provide users with more ease and satisfaction in working with the language.
SL
Hi!
The idea was originally proposed by Marc Easen who created a patch
and asked for help with putting together an RFC. I have yet to see a
formal proposal but on the list Easen modified his idea so that it
should apply to strings alone. With that in mind, would it really
cause problems to have code like this:$string = "Roses are red"; $string[-3] = "R"; // modifying
$string[10] echo $string; // Roses are Red
I see how this may work for strings and simple vectors, but what about this:
$a = array(-1 => "foo", -2 => "bar"); echo $a[-1];
It should keep returning "foo", right? So then the question is - what
$array[-1] actually means? Right now I'm not sure I understand it. I
guess if somebody wrote an RFC on that it'd make understanding it
easier. I see that the patch seems to address just the string part - but
the subject also mentions arrays. I think making it clear would be helpful.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Stas Malyshev wrote:
The idea was originally proposed by Marc Easen who created a patch
and asked for help with putting together an RFC. I have yet to see a
formal proposal but on the list Easen modified his idea so that it
should apply to strings alone. With that in mind, would it really
cause problems to have code like this:$string = "Roses are red"; $string[-3] = "R"; // modifying
$string[10] echo $string; // Roses are Red
I see how this may work for strings and simple vectors, but what about this:$a = array(-1 => "foo", -2 => "bar"); echo $a[-1];
It should keep returning "foo", right? So then the question is - what
$array[-1] actually means? Right now I'm not sure I understand it. I
guess if somebody wrote an RFC on that it'd make understanding it
easier. I see that the patch seems to address just the string part - but
the subject also mentions arrays. I think making it clear would be helpful.
Stanislav I though that the discussion on adding this type of functionality to
strings was already a done deal? Despite the obvious problems it introduces.
If it's not a done deal, then while I can understand why people thing they like
the idea, the clash with other uses of what looks like identical functionality
are good enough reason not to carry it forward.
I've already pointed out why there is no way that this can be discussed in
connection with 'collection arrays' and so there does need to be a clean break
between the two for ANY discussion. I believe that PHP is the only language that
allows us to use -ve indices ( i.e. keys ) in arrays? That was one of the nice
things about PHP when I moved over to it from C and pascal. Introducing two
different styles of working with 'indices' even though they apply to different
types of object does now seem to be adding to the difficult of educating new
users as to why PHP arrays are NOT the same as other array types they may have
used?
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
Hi!
Stanislav I though that the discussion on adding this type of functionality to
strings was already a done deal? Despite the obvious problems it introduces.
I'm not sure what you mean by "done deal". There was this RFC:
https://wiki.php.net/rfc/strncmpnegativelen
but that wasn't implemented due to some objections. There's also a pull
about negative strings offset but no RFC and no vote was ever held on it.
I've already pointed out why there is no way that this can be discussed in
connection with 'collection arrays' and so there does need to be a clean break
between the two for ANY discussion. I believe that PHP is the only language that
allows us to use -ve indices ( i.e. keys ) in arrays? That was one of the nice
Depending what you mean by "arrays". I'm sure any language that has maps
indexed by value would not exclude values equaling to negative numbers.
E.g. in python:
a = {}
a[-2] = 1
a
{-2: 1}
I'm not sure we need or can do anything else for arrays. We maybe could
for strings, technically, though I'm not currently convinced that having
substr()
we really need to. But if the vote goes other way, for strings
I think it may be OK, if we figure out what to do with out-of-the-string
offsets.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Stas Malyshev wrote:
Stanislav I though that the discussion on adding this type of functionality to
strings was already a done deal? Despite the obvious problems it introduces.
I'm not sure what you mean by "done deal". There was this RFC:
https://wiki.php.net/rfc/strncmpnegativelen
but that wasn't implemented due to some objections. There's also a pull
about negative strings offset but no RFC and no vote was ever held on it.
And obviously no 'we are not going to do this' ...
It does get difficult at time to work out what IS still under discussion.
I think I'm getting confused with the extra indicies bits that were added at
some point? Accessing array stored string content by character index ... god
this is getting complicated!
I've already pointed out why there is no way that this can be discussed in
connection with 'collection arrays' and so there does need to be a clean break
between the two for ANY discussion. I believe that PHP is the only language that
allows us to use -ve indices ( i.e. keys ) in arrays? That was one of the nice
Depending what you mean by "arrays". I'm sure any language that has maps
indexed by value would not exclude values equaling to negative numbers.
E.g. in python:a = {}
a[-2] = 1
a
{-2: 1}
I wonder if that was added because of the PHP methods?
I'm not sure we need or can do anything else for arrays. We maybe could
for strings, technically, though I'm not currently convinced that having
substr()
we really need to. But if the vote goes other way, for strings
I think it may be OK, if we figure out what to do with out-of-the-string
offsets.
'Add an exception' ;)
--
Lester Caine - G8HFL
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk
Am Sonntag, 2. September 2012 um 02:43 schrieb slevy1@pipeline.com:
The idea was originally proposed by Marc Easen who created a patch and asked for help with putting together an RFC. I have yet to see a formal proposal but on the list Easen modified his idea so that it should apply to strings alone. With that in mind, would it really cause problems to have code like this:
$string = "Roses are red";
$string[-3] = "R"; // modifying $string[10]
echo $string; // Roses are RedThe negative indexing provides a pleasurable way to easily access part of a string; it's simple and should be fast, too.
I think Easen's proposal is laudatory and I for one would like to see it happen for PHP and provide users with more ease and satisfaction in working with the language.
SL
Hi,
as I understood, a lot of people seem to have problems with this proposal, since arrays and strings could (and maybe should) behave the same.
The main problem arises from the ambiguity for $array[-1] for arrays.
But this is easily solvable: just introduce a slice operator.
$array[:-1] and the ambiguity is gone.
--
Cheers
Jannik Zschiesche
The main problem arises from the ambiguity for $array[-1] for arrays.
But this is easily solvable: just introduce a slice operator.$array[:-1] and the ambiguity is gone.
That would return an array containing the last item as the sole member, not the last item itself.
David
On Tue, Sep 4, 2012 at 6:31 PM, David Zülke david.zuelke@bitextender.comwrote:
The main problem arises from the ambiguity for $array[-1] for arrays.
But this is easily solvable: just introduce a slice operator.$array[:-1] and the ambiguity is gone.
That would return an array containing the last item as the sole member,
not the last item itself.
Assuming Python syntax, that returns the array of items up to (not
including) the last item actually, an array containing the last item would
be $array[-1:] - I wouldn't mind slice syntax :) nifty stuff ... maybe too
nifty.
David
--
--
Tjerk