RE: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-03 Thread Laupretre François
Hi, Instead of [], which is ambiguous, couldn't we just extend the {} syntax : we already have $a{} working with positive numbers, couldn't we extend it to accept negative offsets also ? François -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-02 Thread David Zülke
On 03.09.2012, at 01:11, sle...@pipeline.com wrote: >> 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?

Re: Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-01 Thread Morgan L. Owens
On 2012-09-01 20:17, Kris Craig wrote: This discussion kinda reminds me of some of the debates over AUTO_INCREMENT behavior in the MySQL community. Specifically, they end up having to tackle the same funcamental, conceptual dilemma: If I assign/insert/whatever an arbitrary value to a container

Re: Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-01 Thread Morgan L. Owens
On 2012-09-01 21:23, Sherif Ramadan wrote: $array[0] = 'first element'; $array[9] = 'second element'; var_dump($array); /* array(2) { [9]=> string(14) "second element" [0]=> string(13) "first element" } */ Just correcting this as it was a copy/paste fail... The above code w

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-01 Thread Lester Caine
Lester Caine wrote: Sherif Ramadan wrote: I can't understand what you mean by "Different means of identifying position in the array"? If you mean a way to access an array's element by its position in the array then yes, we already have that. It's called array_slice() seehttp://php.net/array-slic

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-01 Thread Lester Caine
Sherif Ramadan wrote: I can't understand what you mean by "Different means of identifying position in the array"? If you mean a way to access an array's element by its position in the array then yes, we already have that. It's called array_slice() seehttp://php.net/array-slice which allows you t

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-01 Thread Sherif Ramadan
> > $array[0] = 'first element'; > $array[9] = 'second element'; > var_dump($array); > /* > array(2) { > [9]=> > string(14) "second element" > [0]=> > string(13) "first element" > } > */ Just correcting this as it was a copy/paste fail... The above code would produce: array(2) {

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-01 Thread Sherif Ramadan
On Sat, Sep 1, 2012 at 4:52 AM, Lester Caine wrote: > Sherif Ramadan wrote: >> >> As it stands today, that code would result in the following: >> >> $numbers = array(); >> $numbers[-1] = 5; >> $numbers[] = 6; >> var_dump($numbers); >> /* >> array(2) { >>[-1]=> >>int(5) >>[0]=> >>in

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-01 Thread Sherif Ramadan
> > This discussion kinda reminds me of some of the debates over AUTO_INCREMENT > behavior in the MySQL community. Specifically, they end up having to tackle > the same funcamental, conceptual dilemma: If I assign/insert/whatever an > arbitrary value to a container that can be incremented, and th

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-01 Thread Lester Caine
Sherif Ramadan wrote: As it stands today, that code would result in the following: $numbers = array(); $numbers[-1] = 5; $numbers[] = 6; var_dump($numbers); /* array(2) { [-1]=> int(5) [0]=> int(6) } */ I think that it is just clarifying this further by adding $numbers = array(0,1,

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-09-01 Thread Kris Craig
On Fri, Aug 31, 2012 at 10:24 PM, Sherif Ramadan wrote: > > > > That might actually be something I could use :) But the fun for me > begins here: > > > > $numbers = array(); > > $numbers[-1] = 5; > > $numbers[] = 6; > > > > What would have happened to the keys? Normally [] is equivalent to > [coun

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-08-31 Thread Sherif Ramadan
> > That might actually be something I could use :) But the fun for me begins > here: > > $numbers = array(); > $numbers[-1] = 5; > $numbers[] = 6; > > What would have happened to the keys? Normally [] is equivalent to > [count($numbers)]. > This is incorrect, $numbers[] = 6; is not equivalent t

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-08-31 Thread Tjerk Meesters
On 1 Sep, 2012, at 1:11 AM, Rasmus Schultz wrote: > Yes, typo! sorry. > > $array = array(1001, 1002, 1003, 1004); > $number = $array[-1]; // => 1004 > $array[-1] = 1005; > $number = $array[-1]; // => > > Looking at the resulting code, I would like to point out also that >

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-08-31 Thread Rasmus Schultz
Yes, typo! sorry. $array = array(1001, 1002, 1003, 1004); $number = $array[-1]; // => 1004 $array[-1] = 1005; $number = $array[-1]; // => Looking at the resulting code, I would like to point out also that it's extremely misleading... because $array[-1] references two comp

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-08-31 Thread Ferenc Kovacs
On Fri, Aug 31, 2012 at 3:14 PM, Rasmus Schultz wrote: > Having thought about this for a while, I think this is a bad idea - here's > why: > > $array = array(1001, 1002, 1003, 1004); > > $number = $array[-1]; // => 1004 > > $number[-1] = 1005; > > $number = $array[-1]; // =>

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-08-31 Thread Rasmus Schultz
From: Marc Easen To: "internals@lists.php.net" , Lars Strojny Cc: Date: Sun, 17 Jun 2012 20:53:38 +0100 Subject: Re: [PHP-DEV] Support negative indexes for arrays and strings Hi Lars, I don't think there needs to be a new operator, as it is possible to achieve this behaviour wit

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-06-17 Thread Lars Strojny
Hi Marc, Am 17.06.2012 um 21:53 schrieb Marc Easen: [...] > Numerical keyed array: > >$a = array('foo', 'bar', 'baz'); >$a[0] === 'foo' > > I would expect: > >$a[-1] === 'baz'; > > An string keyed array: > >$b = array('foo' => 1, 'bar' => 2, 'baz' => 3); >$b[0] === 1; >

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-06-17 Thread Marc Easen
Hi Lars, I don't think there needs to be a new operator, as it is possible to achieve this behaviour without adding a new language construct. The odd thing with PHP as I'm sure you are aware of is that arrays can be either a lists or a dictionary. This is where this becomes quite difficult to

[PHP-DEV] Support negative indexes for arrays and strings

2012-06-12 Thread slevy1
I feel that for PHP to incorporate some of the convenience that Python offers is a good particularly with respect to attracting the upcoming generation. PHP needs an injection of new coolness to attract and captivate the imagination of young college-age people who are being exposed to Python as

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-06-11 Thread Marc Easen
On 11/06/12 20:13, Stas Malyshev wrote: Hi! Can be: $var = 'abc'; echo $var[-1]; This seems simple enough for a hard-coded -1, but... Would $var[-2] be strlen($var) - 2 and so on? The main question is what happens with "foo"[-4] or ['x'][-2]. And then one would expect some rather complex l

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-06-11 Thread Marc Easen
On 11/06/12 20:28, Richard Lynch wrote: On Mon, June 11, 2012 2:13 pm, Stas Malyshev wrote: And then one would expect some rather complex logic to compute -N for $var[-N] I don't see much of complex logic here, but $a[2] = 'a' would create a new array element if it does not exist, while $a[-2]

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-06-11 Thread Galen Wright-Watson
On Mon, Jun 11, 2012 at 12:13 PM, Stas Malyshev wrote: > Hi! > > >> Can be: > >> $var = 'abc'; > >> echo $var[-1]; > > > > This seems simple enough for a hard-coded -1, but... > > > > Would $var[-2] be strlen($var) - 2 and so on? > > The main question is what happens with "foo"[-4] or ['x'][-2]. >

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-06-11 Thread Richard Lynch
On Mon, June 11, 2012 2:13 pm, Stas Malyshev wrote: >> And then one would expect some rather complex logic to compute -N >> for >> $var[-N] > > I don't see much of complex logic here, but $a[2] = 'a' would create a > new array element if it does not exist, while $a[-2] can't. Not a big > issue, but

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-06-11 Thread Stas Malyshev
Hi! >> Can be: >> $var = 'abc'; >> echo $var[-1]; > > This seems simple enough for a hard-coded -1, but... > > Would $var[-2] be strlen($var) - 2 and so on? The main question is what happens with "foo"[-4] or ['x'][-2]. > And then one would expect some rather complex logic to compute -N for >

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-06-11 Thread Richard Lynch
On Mon, June 4, 2012 2:08 pm, Marc Easen wrote: > I have submitted a patch to support negative indexs in strings, as per > the conversation adding them to arrays could possibly detract from the > syntactical sugar they are indented to be. > > In summary: > > An alternative to: > $var = 'abc'; > ech

Re: [PHP-DEV] Support negative indexes for arrays and strings

2012-06-04 Thread Marc Easen
I have submitted a patch to support negative indexs in strings, as per the conversation adding them to arrays could possibly detract from the syntactical sugar they are indented to be. In summary: An alternative to: $var = 'abc'; echo $var[strlen($var) - 1]; Can be: $var = 'abc'; echo $var[-1

Re: [PHP-DEV] Support negative indexes for arrays and strings

2011-01-14 Thread Stas Malyshev
Hi! I would like to open the discussion around the support of negative indexes, as I feel a lot of developers will benefit from this syntactical sugar. What you are looking for is implemented in array_slice(). Python has shortcuts for this thing, like a[1:-1], but PHP doesn't. If you wanted to

Re: [PHP-DEV] Support negative indexes for arrays and strings

2011-01-14 Thread Marcin Babij
> Hello everyone, > > I would like to open the discussion around the support of negative indexes, > as I feel a lot of developers will benefit from this syntactical sugar. > > Example: > > echo $foo[2]; // 3 > echo $foo[-1]; // 3 Negative indexes are used already - for negative indexes. What shou

Re: [PHP-DEV] Support negative indexes for arrays and strings

2011-01-14 Thread Matthew Fonda
On Fri, Jan 14, 2011 at 10:21 AM, Marc Easen wrote: > Hello everyone, > > I would like to open the discussion around the support of negative indexes, > as I feel a lot of developers will benefit from this syntactical sugar. It would be convenient, but PHP already allows arrays to have negative i

[PHP-DEV] Support negative indexes for arrays and strings

2011-01-14 Thread Marc Easen
Hello everyone, I would like to open the discussion around the support of negative indexes, as I feel a lot of developers will benefit from this syntactical sugar. Example: $foo = array(1,2,3); echo $foo[2]; // 3 echo $foo[-1]; // 3 $bar = 'baz'; echo $foo[2]; // 'z' echo $foo[-1]; // 'z' The