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 #185307 is a reply to message #185306] Mon, 17 March 2014 13:47 Go to previous messageGo to previous message
Gabriel is currently offline  Gabriel
Messages: 11
Registered: March 2014
Karma:
Junior Member
On 2014-03-17 13:32:57 +0000, Thomas 'PointedEars' Lahn said:

> Gabe wrote:
> ^^^^
> Please fix.
>
>> 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
>> […]
>> )
>
> Which *still* is _not_ what was asked for, which is (for whatever reason)
> keys from _1_ to _40_ inclusive.
>
> It is also less than optimal to store the repeating prefix in the array, but
> you might only have gotten the idea that this was wanted from the precursor.
>
>> 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. […]
>
> You are missing the point.
>
>
> PointedEars

Dear PointedEars

I am so sorry - I sometimes assume that people (on a PHP dev mailing
list) would be able to make very basic adjustments to snippets that I
posted to accommodate for discrepencies. I take it you are not one and
have not programmed PHP before and are just looking to get started.

Allow me to show you how to make such a change in order to produce an
array index 1 to 40 inclusive… You will need to change:

$arr = array_fill(0, 40, 'http://mroldies.net/audio/19%u/%u-%03u.mp3');

to

$arr = array_fill(1, 40, 'http://mroldies.net/audio/19%u/%u-%03u.mp3');

There is no problem storing a repeated prefix in the initial array as
the output array will contain it anyway. Since the value is replaced
in-situ memory usage would be approximately the same. Infact there is
only one array that is programmatically manipulated. If the URL is not
required to be stored in the array then please ask me for help again
and I will be glad to show you how to delete the characters comprising
the URL from the snippet.

Cheers

Gabe
[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: Wed Nov 27 21:53:02 GMT 2024

Total time taken to generate the page: 0.04041 seconds