Re: Proposal: Array syntax

php.internals

Jaap van Ganswijk

22 years ago
At 2003-11-05 09:59 +0100, Michael Walter wrote:
>Very cool. > >How about supporting .. syntax, btw. as in [1..3] or ["a".."z"]? Might no be the worth, just thinking out loud ;)
I'm also in favor of a shorter notation for array() and list(). In fact this also helps to ease the problem I have with 'foreach ($A as $I=>$d)', because it would then be possible to write: while ([$i,$d]=each($A)). It's still not as short as I'd like it to be: 'while ($i=>$d in $A)', but it's already a lot better. I'd also like to see ranges supported and propose to add notations for inclusive and non-inclusive ranges as: 1..3 inclusive at both ends so equal to 1,2,3 1.<3 non inclusive at the end so equal to 1,2 1>.3 non inclusive at the beginning so equal to 2,3 1><3 non inclusive at both ends so equal to 2 Of course generally only the first two variations are being used. (Please note that this syntax conflicts slightly with the syntax of floating point numbers and compares. Perhaps it's solvable by scanning '..', '.<', '>.' and '><' as tokens first and not allowing ranges with floating point numbers anyway.) By the way, Python has three 'group' notations: [] for a list {} for a dictionary () for a tuple Very confusing (and one of the reasons I prefer PHP over Python ;-), and I agree with Rasmus that at least here the symbols no longer make clear what is being meant, but I think that just [] for the values in an array is very intuitive because the same symbols are being used to add the index of an array. (But {} from the set notion in mathematics would be fine too, I think). By the way, don't forget to allow a comma at the end of a list: 1,2,3, This especially makes sense when the elements are listed each on a seperate line: 1, 2, 3, Greetings, Jaap

Andrey Hristov

22 years ago
Jaap van Ganswijk wrote:
>At 2003-11-05 09:59 +0100, Michael Walter wrote: > > >>Very cool. >> >>How about supporting .. syntax, btw. as in [1..3] or ["a".."z"]? Might no be the worth, just thinking out loud ;) >> >> > >I'm also in favor of a shorter notation for array() and list(). > >In fact this also helps to ease the problem I have >with 'foreach ($A as $I=>$d)', because it would >then be possible to write: while ([$i,$d]=each($A)). > >
Do you know that while(list(..,..) = each($ar)) is kinda "deprecated". You should consider using foreach() which is clearer and the fastest way to traverse an array. while() + each() + list() is the old way (evermore, you have not to forget to reset() your array before the traversal - foreach() does not need this step). Andrey
>It's still not as short as I'd like it to be: >'while ($i=>$d in $A)', but it's already a lot >better. > >I'd also like to see ranges supported and propose >to add notations for inclusive and non-inclusive >ranges as: >1..3 inclusive at both ends so equal to 1,2,3 > > >1.<3 non inclusive at the end so equal to 1,2 >1>.3 non inclusive at the beginning so equal to 2,3 >1><3 non inclusive at both ends so equal to 2 > >
Use the range() function. PHP is more close to C than to Pascal in my opinion. The language should be simple the standard library should be rich (IMO). Andrey