Re: Filling an array with random input doesn't quite work [message #185270 is a reply to message #185268] |
Sun, 16 March 2014 13:46 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 3/15/2014 10:51 PM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>
>> On 3/15/2014 5:32 PM, Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>>>
>>>> Daniel Pitts wrote:
> <snip>
>>>> > for ($x = 1; $x < 40; ++$x) {
>>>> > $ayr[$x] = rand(60, 69);
>>>> > }
>>>> >
>>>> > So much simpler, more efficient, and actually correct.
>>>>
>>>> No, this will produce an array of only 39 elements, assuming there was not
>>>> already one. This can be easily fixed, of course.
>>>>
>>>> Still,
>>>>
>>>> $keys = range(1, 40);
>>>> $ayr = array_combine($keys, array_map(function () {
>>>> return rand(60, 69);
>>>> }, $keys)));
>>>>
>>>> is much more elegant :)
>>>
>>> Yes, but it's a shame about the extra ')'. I think
>>>
>>> $ayr = array_map(function () { return rand(60, 69); },
>>> array_fill(1, 40, null));
>>>
>>> is a little simpler.
>>>
>>
>> Shorter, maybe - but arguably NOT simpler. Hand that code to a new
>> PHP programmer and he/she will have no idea.
>
> Surely that would depend on their background would it not? Anyway, I
> meant simpler that the version using array_combine and array_map.
>
No, it does not. You may not be the only one looking at it.
>> It's generally better to code for clarity first.
>
> Yes, I agree, but clarity is not an absolute, and aiming to be clear to
> beginners puts considerable constraints on design and coding.
>
Not at all. But not being able to be clear in coding shows a lack of
good programming skills.
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
|
|
|