Re: Filling an array with random input doesn't quite work [message #185266 is a reply to message #185265] |
Sat, 15 March 2014 22:14 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
Ben Bacarisse wrote:
> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>> Daniel Pitts wrote:
>>> 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 ')'.
Ah, yes, no idea how that got in there. Should have been
$keys = range(1, 40);
$ayr = array_combine($keys, array_map(function () {
return rand(60, 69);
}, $keys));
> I think
>
> $ayr = array_map(function () { return rand(60, 69); },
> array_fill(1, 40, null));
>
> is a little simpler.
ACK. Actually, I had experimented with array_fill() but, strangely enough,
I overlooked this possibility. (I had posted something similar with regard
to letters not long ago.)
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
|
|
|