FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Filling an array with random input doesn't quite work
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Filling an array with random input doesn't quite work [message #185305 is a reply to message #185304] Mon, 17 March 2014 12:52 Go to previous messageGo to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma:
Senior Member
On 3/17/2014 8:43 AM, Gabe wrote:
> On 2014-03-17 02:16:24 +0000, Denis McMahon said:
>
>>> I would do it like this:
>>>
>>> $arr = array_fill(0, 40, 0);
>>> array_walk($arr, function(&$v, $k) { $v = rand(60, 69); });
>>>
>>> That will provide you with an array containing 40 elements, each a
>>> random integer between 60 and 69:
>>>
>>>
>>> print_r($arr);
>>> Array (
>>> [0] => 60 [1] => 69 [2] => 63 [3] => 63 [4] => 64 [5] => 65 [6] =>
>>> 68 [7] => 67 [8] => 67 [9] => 67 [10] => 63 [11] => 69 [12] => 65
>>> [13] => 60 [14] => 68 [15] => 62 [16] => 67 [17] => 65 [18] => 60
>>> [19] => 65 [20] => 62 [21] => 69 [22] => 69 [23] => 64 [24] => 68
>>> [25] => 65 [26] => 69 [27] => 66 [28] => 61 [29] => 69 [30] => 61
>>> [31] => 61 [32] => 68 [33] => 64 [34] => 64 [35] => 63 [36] => 60
>>> [37] => 63 [38] => 60 [39] => 67
>>> )
>>
>> Yes, but what he wants is actually something like:
>>
>> Array
>> (
>> [39] => http://mroldies.net/audio/1966/66-033.mp3
>> [38] => http://mroldies.net/audio/1964/64-012.mp3
>> [37] => http://mroldies.net/audio/1963/63-085.mp3
>> [36] => http://mroldies.net/audio/1960/60-014.mp3
>> [35] => http://mroldies.net/audio/1960/60-011.mp3
>> [34] => http://mroldies.net/audio/1964/64-065.mp3
>
> Thank you for pointing that out, Dennis.
>
> My code can be trivially modified to do this:
>
>
> $arr = array_fill(0, 40, 'http://mroldies.net/audio/19%u/%u-%03u.mp3');
> array_walk($arr, function(&$v, $k) { $yr = rand(60, 69); $v =
> sprintf($v, $yr, $yr, rand(1, 100)); });
>
>
> Will yield the following array which I have cut short to reduce noise:
>
> print_r($arr);
> Array
> (
> [0] => http://mroldies.net/audio/1966/66-007.mp3
> [1] => http://mroldies.net/audio/1967/67-021.mp3
> [2] => http://mroldies.net/audio/1962/62-016.mp3
> […]
> )
>
> Each person prefers their own solution and I am no different. I prefer
> mine because I find it readable whilst only requiring two lines, of
> course you could add a few more line breaks in if preferable. The
> highlights for me are utilising a PHP native function to walk the array
> instead of writing your own iterator using some form of while or for
> loop, a lambda function for the processing, and the fantastic formatting
> abilities of sprintf.
>
> Cheers
>
> Gabe
>

The question here is not whether YOU find it readable - does the NEW GUY
find it readable?

I've seen more bugs in code because people try to get fancy or short
than when they try to be clear. Good programmers understand this.

--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex(at)attglobal(dot)net
==================
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: readdir lists randomly
Next Topic: Nested PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 06:40:54 GMT 2024

Total time taken to generate the page: 0.05130 seconds