FUDforum - خوراک RDF
http://fudforum.org/forum/index.php
Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185211&th=123442#msg_185211
while ($x<40){
$yr=range(60,69);
shuffle($yr);
$ayr[$x]=$yr[$x];
echo $ayr[$x]."<br>";
$x++;
}
I am attempting to load an array based upon the range of numbers 60 through
69.
The array will have 40 items.
This code produces 9 items just fine.
The rest of the array is empty.
What am I missing?]]>Mr Oldies2014-03-12T17:18:19-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185212&th=123442#msg_185212
12lin35i3xsev.abk024ld5l8l.dlg@40tude.net>, richard
<noreply@example.com> wrote:
> $x=1;
> while ($x<40){
> $yr=range(60,69);
> shuffle($yr);
> $ayr[$x]=$yr[$x];
> echo $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60 through
> 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
This:
$ayr[$x]=$yr[$x];
isn't gonna work once $x gets above 9, now is it? Your array $yr only
contains 10 items.
--
"I am enclosing two tickets to the first night of my new play; bring a
friend.... if you have one." - GB Shaw to Churchill "Cannot possibly
attend first night, will attend second... if there is one." - Winston
Churchill, in response.]]>Tim Streater2014-03-12T17:36:58-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185213&th=123442#msg_185213
noreply@example.com> wrote in news:12lin35i3xsev.abk024ld5l8l.dlg@
40tude.net:
> $x=1;
> while ($x<40){
> $yr=range(60,69);
> shuffle($yr);
> $ayr[$x]=$yr[$x];
> echo $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60 through
> 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
This line:
$ayr[$x]=$yr[$x];
What is the "value" of $yr[$x] when x is greater than 9?]]>Doug Miller2014-03-12T19:02:30-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185214&th=123442#msg_185214
noreply@example.com> wrote in news:12lin35i3xsev.abk024ld5l8l.dlg@
40tude.net:
> $x=1;
> while ($x<40){
> $yr=range(60,69);
> shuffle($yr);
> $ayr[$x]=$yr[$x];
> echo $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60 through
> 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
This may be helpful -- specifically the % operator.
http://www.php.net/manual/en/language.operators.arithmetic.php]]>Doug Miller2014-03-12T19:06:58-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185215&th=123442#msg_185215
> $x=1;
> while ($x<40){
> $yr=range(60,69); shuffle($yr); $ayr[$x]=$yr[$x]; echo
> $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60
> through 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
You're missing a richardian fuck up in the array indexing!
You also seem to be ignoring a message something like:
"PHP Notice: Undefined offset: 38 in /richardian_array_fuckup.php on
line 13"
Try:
$x=1;
while ($x<40){
$yr=range(60,69);
shuffle($yr);
$n = count($yr);
echo "\$x is {$x}, size of \$yr is {$n} elements, \$yr[\$x] is '{$yr
[$x]}'<br>\n";
$ayr[$x]=$yr[$x];
echo $ayr[$x]."<br>\n";
$x++;
}
assuming that what you actually want is an array of 39 elements indexed
1..39 with random values from 60 to 69.
I doubt that's exactly what you do want, but that's my best guess at what
your code is intended to create.
--
Denis McMahon, denismfmcmahon@gmail.com]]>Denis McMahon2014-03-12T20:34:57-00:00solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185216&th=123442#msg_185216
> $x=1;
> while ($x<40){
> $yr=range(60,69);
> shuffle($yr);
> $ayr[$x]=$yr[$x];
> echo $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60 through
> 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
At this point though, I don't have it worked out to check for duplicats.]]>Mr Oldies2014-03-12T21:08:20-00:00Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185217&th=123442#msg_185217
<noreply@example.com> wrote:
You're welcome.
> $x=1;
> while ($x<40){
> $yr=rand(1,10);
> $yr=$yr+59;
>
> $num=rand(1,100);
> if ($num<100){$num="0".$num;}
> if ($num<10){$num="0".$num;}
What is the point if this second "if"? You've already covered that with
the first.
--
"The idea that Bill Gates has appeared like a knight in shining armour to
lead all customers out of a mire of technological chaos neatly ignores
the fact that it was he who, by peddling second-rate technology, led them
into it in the first place." - Douglas Adams]]>Tim Streater2014-03-12T22:49:38-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185218&th=123442#msg_185218
2@dont-email.me>, Denis McMahon
<denismfmcmahon@gmail.com> wrote:
> assuming that what you actually want is an array of 39 elements indexed
> 1..39 with random values from 60 to 69.
And of course he will, unavoidably, get duplicates.
--
"I am enclosing two tickets to the first night of my new play; bring a
friend.... if you have one." - GB Shaw to Churchill "Cannot possibly
attend first night, will attend second... if there is one." - Winston
Churchill, in response.]]>Tim Streater2014-03-12T22:51:18-00:00Re: solved! Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185219&th=123442#msg_185219
> In article <85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>, richard
> <noreply@example.com> wrote:
>
> You're welcome.
>
>> $x=1;
>> while ($x<40){
>> $yr=rand(1,10);
>> $yr=$yr+59;
>> $num=rand(1,100);
>> if ($num<100){$num="0".$num;}
>> if ($num<10){$num="0".$num;}
>
> What is the point if this second "if"? You've already covered that with
> the first.
That serves to add padding to three characters. The last three lines
could be replaced with the following:
--
Christoph M. Becker]]>Christoph Michael Bec2014-03-13T00:11:59-00:00Re: solved! Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185220&th=123442#msg_185220
> In article <85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>, richard
> <noreply@example.com> wrote:
>
> You're welcome.
>
>> $x=1;
>> while ($x<40){
>> $yr=rand(1,10);
>> $yr=$yr+59;
>>
>> $num=rand(1,100);
>> if ($num<100){$num="0".$num;}
>> if ($num<10){$num="0".$num;}
>
> What is the point if this second "if"? You've already covered that with
> the first.
>
I do it this way because I've been doing the same method in BASIC for
years. It works fine.
It also helps for sorting purposes.
by having all the strings the same length helps to guarantee that sorting
will not screw it up.
When the number equals 1,
the first "if" adds a zero.
The second "if" adds another zero because it is less than 10.
numbers between 10 and 100 only get one zero added.]]>Mr Oldies2014-03-13T01:14:03-00:00Re: solved! Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185221&th=123442#msg_185221
> Tim Streater wrote:
>
>> In article <85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>, richard
>> <noreply@example.com> wrote:
>>
>> You're welcome.
>>
>>> $x=1;
>>> while ($x<40){
>>> $yr=rand(1,10);
>>> $yr=$yr+59;
>>> $num=rand(1,100);
>>> if ($num<100){$num="0".$num;}
>>> if ($num<10){$num="0".$num;}
>>
>> What is the point if this second "if"? You've already covered that with
>> the first.
>
> That serves to add padding to three characters. The last three lines
> could be replaced with the following:
>
> str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);
>
>>>
>>> $song[$x]="http://mroldies.net/audio/19$yr/".$yr."-".$num.".mp3";
>>> $x++;
>>> }
thanks for the clue. I may use it.]]>Mr Oldies2014-03-13T02:43:56-00:00Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185222&th=123442#msg_185222
timstreater@greenbee.net> wrote in news:120320142249386192% timstreater@greenbee.net:
> In article <85dkidwa0h67.ecj3ezgrqtzk$.dlg@40tude.net>, richard
> <noreply@example.com> wrote:
>
> You're welcome.
>
>> $x=1;
>> while ($x<40){
>> $yr=rand(1,10);
>> $yr=$yr+59;
>>
>> $num=rand(1,100);
>> if ($num<100){$num="0".$num;}
>> if ($num<10){$num="0".$num;}
>
> What is the point if this second "if"? You've already covered that with
> the first.
No, he hasn't. The point is to pad $num on the left with zeros to a consistent length of 3
characters. Values less than 100 are padded with a zero; values less than 10 are padded
again -- so anything between 1 and 9 inclusive gets two padding characters, and values
from 10 to 99 get only one.]]>Doug Miller2014-03-13T02:50:17-00:00Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185223&th=123442#msg_185223
noreply@example.com> wrote in news:85dkidwa0h67.ecj3ezgrqtzk$.dlg@
40tude.net:
> $yr=rand(1,10);
> $yr=$yr+59;
Why not this instead?
$yr = rand(60, 69);]]>Doug Miller2014-03-13T02:53:08-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185225&th=123442#msg_185225
> $x=1;
> while ($x<40){
> $yr=range(60,69);
> shuffle($yr);
> $ayr[$x]=$yr[$x];
> echo $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60 through
> 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
>
for ($items=1; $items<=40; $items++)
{
$ayr[$items] = range(60,69);
shuffle(&$ayr[$items]);
}
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-]]>Norman Peelman2014-03-13T03:02:05-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185226&th=123442#msg_185226
> On 03/12/2014 01:18 PM, richard wrote:
>> $x=1;
>> while ($x<40){
>> $yr=range(60,69);
>> shuffle($yr);
>> $ayr[$x]=$yr[$x];
>> echo $ayr[$x]."<br>";
>> $x++;
>> }
>>
>>
>> I am attempting to load an array based upon the range of numbers 60
>> through
>> 69.
>> The array will have 40 items.
>> This code produces 9 items just fine.
>> The rest of the array is empty.
>> What am I missing?
>>
>
> for ($items=1; $items<=40; $items++)
> {
> $ayr[$items] = range(60,69);
> shuffle(&$ayr[$items]);
> }
>
>
Why would you shuffle a single item?
--
==================
Remove the "x" from my email address
Jerry Stuckle jstucklex@attglobal.net
==================]]>Jerry Stuckle2014-03-13T03:11:50-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185229&th=123442#msg_185229
> On 03/12/2014 01:18 PM, richard wrote:
>> $x=1;
>> while ($x<40){
>> $yr=range(60,69);
>> shuffle($yr);
>> $ayr[$x]=$yr[$x];
>> echo $ayr[$x]."<br>";
>> $x++;
>> }
>>
>>
>> I am attempting to load an array based upon the range of numbers 60
>> through
>> 69.
>> The array will have 40 items.
>> This code produces 9 items just fine.
>> The rest of the array is empty.
>> What am I missing?
>>
>
> for ($items=1; $items<=40; $items++)
> {
> $ayr[$items] = range(60,69);
> shuffle(&$ayr[$items]);
> }
>
>
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-]]>Norman Peelman2014-03-13T04:31:03-00:00Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work)
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185232&th=123442#msg_185232
> Ricjard Teh Stoopid tried to code something, and this is what escaped
to the internet:
>> $num=rand(1,100);
>> if ($num<100){$num="0".$num;}
>> if ($num<10){$num="0".$num;}
> What is the point if this second "if"? You've already covered that with
> the first.
--
Denis McMahon, denismfmcmahon@gmail.com]]>Denis McMahon2014-03-13T16:23:19-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185241&th=123442#msg_185241
> On 3/12/2014 11:02 PM, Norman Peelman wrote:
>> On 03/12/2014 01:18 PM, richard wrote:
>>> $x=1;
>>> while ($x<40){
>>> $yr=range(60,69);
>>> shuffle($yr);
>>> $ayr[$x]=$yr[$x];
>>> echo $ayr[$x]."<br>";
>>> $x++;
>>> }
>>>
>>>
>>> I am attempting to load an array based upon the range of numbers 60
>>> through
>>> 69.
>>> The array will have 40 items.
>>> This code produces 9 items just fine.
>>> The rest of the array is empty.
>>> What am I missing?
>>>
>>
>> for ($items=1; $items<=40; $items++)
>> {
>> $ayr[$items] = range(60,69);
>> shuffle(&$ayr[$items]);
>> }
>>
>>
>
> Why would you shuffle a single item?
>
>
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-]]>Norman Peelman2014-03-14T13:31:54-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185242&th=123442#msg_185242
> On 03/12/2014 11:11 PM, Jerry Stuckle wrote:
>> On 3/12/2014 11:02 PM, Norman Peelman wrote:
>>> On 03/12/2014 01:18 PM, richard wrote:
>>>> $x=1;
>>>> while ($x<40){
>>>> $yr=range(60,69);
>>>> shuffle($yr);
>>>> $ayr[$x]=$yr[$x];
>>>> echo $ayr[$x]."<br>";
>>>> $x++;
>>>> }
>>>>
>>>>
>>>> I am attempting to load an array based upon the range of numbers 60
>>>> through
>>>> 69.
>>>> The array will have 40 items.
>>>> This code produces 9 items just fine.
>>>> The rest of the array is empty.
>>>> What am I missing?
>>>>
>>>
>>> for ($items=1; $items<=40; $items++)
>>> {
>>> $ayr[$items] = range(60,69);
>>> shuffle(&$ayr[$items]);
>>> }
>>>
>>>
>>
>> Why would you shuffle a single item?
>>
>>
>
> $ayr[$items] is an Array
>
> php > print_r($ayr);
> Array
> (
> [1] => Array
> (
> [0] => 60
> [1] => 61
> [2] => 62
> [3] => 63
> [4] => 64
> [5] => 65
> [6] => 66
> [7] => 67
> [8] => 68
> [9] => 69
> )
>
> )
>
> ... after Shuffle:
>
> php > shuffle(&$ayr[1]);
> php > print_r($ayr[1]);
> Array
> (
> [0] => 67
> [1] => 69
> [2] => 66
> [3] => 61
> [4] => 62
> [5] => 60
> [6] => 64
> [7] => 68
> [8] => 63
> [9] => 65
> )
>
>
>
Sorry, I was in a hurry and wasn't paying as much attention.
But then why have 40 different arrays of 10 items each, anyway?
--
==================
Remove the "x" from my email address
Jerry Stuckle jstucklex@attglobal.net
==================]]>Jerry Stuckle2014-03-14T15:00:19-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185243&th=123442#msg_185243
> On 3/14/2014 9:31 AM, Norman Peelman wrote:
>> On 03/12/2014 11:11 PM, Jerry Stuckle wrote:
>>> On 3/12/2014 11:02 PM, Norman Peelman wrote:
>>>> On 03/12/2014 01:18 PM, richard wrote:
>>>> > $x=1;
>>>> > while ($x<40){
>>>> > $yr=range(60,69);
>>>> > shuffle($yr);
>>>> > $ayr[$x]=$yr[$x];
>>>> > echo $ayr[$x]."<br>";
>>>> > $x++;
>>>> > }
>>>> >
>>>> >
>>>> > I am attempting to load an array based upon the range of numbers 60
>>>> > through
>>>> > 69.
>>>> > The array will have 40 items.
>>>> > This code produces 9 items just fine.
>>>> > The rest of the array is empty.
>>>> > What am I missing?
>>>> >
>>>>
>>>> for ($items=1; $items<=40; $items++)
>>>> {
>>>> $ayr[$items] = range(60,69);
>>>> shuffle(&$ayr[$items]);
>>>> }
>>>>
>>>>
>>>
>>> Why would you shuffle a single item?
>>>
>>>
>>
>> $ayr[$items] is an Array
>>
>> php > print_r($ayr);
>> Array
>> (
>> [1] => Array
>> (
>> [0] => 60
>> [1] => 61
>> [2] => 62
>> [3] => 63
>> [4] => 64
>> [5] => 65
>> [6] => 66
>> [7] => 67
>> [8] => 68
>> [9] => 69
>> )
>>
>> )
>>
>> ... after Shuffle:
>>
>> php > shuffle(&$ayr[1]);
>> php > print_r($ayr[1]);
>> Array
>> (
>> [0] => 67
>> [1] => 69
>> [2] => 66
>> [3] => 61
>> [4] => 62
>> [5] => 60
>> [6] => 64
>> [7] => 68
>> [8] => 63
>> [9] => 65
>> )
>>
>>
>>
>
> Sorry, I was in a hurry and wasn't paying as much attention.
>
> But then why have 40 different arrays of 10 items each, anyway?
>
I don't even know why there are 40 items when you are pulling from a
selection of 10 - there's going to be duplicates. Maybe that doesn't
matter... I think someone else actually had what Richard was really after.
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-]]>Norman Peelman2014-03-14T15:47:53-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185244&th=123442#msg_185244
> On 03/14/2014 11:00 AM, Jerry Stuckle wrote:
>> On 3/14/2014 9:31 AM, Norman Peelman wrote:
>>> On 03/12/2014 11:11 PM, Jerry Stuckle wrote:
>>>> On 3/12/2014 11:02 PM, Norman Peelman wrote:
>>>> > On 03/12/2014 01:18 PM, richard wrote:
>>>> >> $x=1;
>>>> >> while ($x<40){
>>>> >> $yr=range(60,69);
>>>> >> shuffle($yr);
>>>> >> $ayr[$x]=$yr[$x];
>>>> >> echo $ayr[$x]."<br>";
>>>> >> $x++;
>>>> >> }
>>>> >>
>>>> >>
>>>> >> I am attempting to load an array based upon the range of numbers 60
>>>> >> through
>>>> >> 69.
>>>> >> The array will have 40 items.
>>>> >> This code produces 9 items just fine.
>>>> >> The rest of the array is empty.
>>>> >> What am I missing?
>>>> >>
>>>> >
>>>> > for ($items=1; $items<=40; $items++)
>>>> > {
>>>> > $ayr[$items] = range(60,69);
>>>> > shuffle(&$ayr[$items]);
>>>> > }
>>>> >
>>>> >
>>>>
>>>> Why would you shuffle a single item?
>>>>
>>>>
>>>
>>> $ayr[$items] is an Array
>>>
>>> php > print_r($ayr);
>>> Array
>>> (
>>> [1] => Array
>>> (
>>> [0] => 60
>>> [1] => 61
>>> [2] => 62
>>> [3] => 63
>>> [4] => 64
>>> [5] => 65
>>> [6] => 66
>>> [7] => 67
>>> [8] => 68
>>> [9] => 69
>>> )
>>>
>>> )
>>>
>>> ... after Shuffle:
>>>
>>> php > shuffle(&$ayr[1]);
>>> php > print_r($ayr[1]);
>>> Array
>>> (
>>> [0] => 67
>>> [1] => 69
>>> [2] => 66
>>> [3] => 61
>>> [4] => 62
>>> [5] => 60
>>> [6] => 64
>>> [7] => 68
>>> [8] => 63
>>> [9] => 65
>>> )
>>>
>>>
>>>
>>
>> Sorry, I was in a hurry and wasn't paying as much attention.
>>
>> But then why have 40 different arrays of 10 items each, anyway?
>>
>
> I don't even know why there are 40 items when you are pulling from a
> selection of 10 - there's going to be duplicates. Maybe that doesn't
> matter... I think someone else actually had what Richard was really after.
>
>
>
But each item in your list is an array. He wants single values.
--
==================
Remove the "x" from my email address
Jerry Stuckle jstucklex@attglobal.net
==================]]>Jerry Stuckle2014-03-14T16:55:33-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185250&th=123442#msg_185250
> $x=1;
> while ($x<40){
> $yr=range(60,69);
> shuffle($yr);
> $ayr[$x]=$yr[$x];
> echo $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60 through
> 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
>
So much simpler, more efficient, and actually correct.]]>Daniel Pitts2014-03-14T19:42:32-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185254&th=123442#msg_185254
> On 3/12/14 10:18 AM, richard wrote:
>> $x=1;
>> while ($x<40){
>> $yr=range(60,69);
>> shuffle($yr);
>> $ayr[$x]=$yr[$x];
>> echo $ayr[$x]."<br>";
>> $x++;
>> }
>>
>> I am attempting to load an array based upon the range of numbers 60
>> through 69.
>> The array will have 40 items.
>> This code produces 9 items just fine.
>> The rest of the array is empty.
>> What am I missing?
>
> 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.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann]]>Thomas 'PointedEars' 2014-03-14T22:07:00-00:00Re: solved! Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185257&th=123442#msg_185257
> Tim Streater wrote:
>> richard <noreply@example.com> wrote:
>>> $x=1;
>>> while ($x<40){
>>> $yr=rand(1,10);
>>> $yr=$yr+59;
>>> $num=rand(1,100);
>>> if ($num<100){$num="0".$num;}
>>> if ($num<10){$num="0".$num;}
>>
>> What is the point if this second "if"? You've already covered that with
>> the first.
>
> That serves to add padding to three characters. The last three lines
> could be replaced with the following:
>
> str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);
as str_pad() does not modify strings (because they are immutable).
Or simply
$num = sprintf('%03d', rand(1, 100));
:)
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@news.demon.co.uk>]]>Thomas 'PointedEars' 2014-03-14T22:23:42-00:00Re: solved! Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185258&th=123442#msg_185258
> Christoph Michael Becker wrote:
>
>> Tim Streater wrote:
>>> richard <noreply@example.com> wrote:
>>>> $x=1;
>>>> while ($x<40){
>>>> $yr=rand(1,10);
>>>> $yr=$yr+59;
>>>> $num=rand(1,100);
>>>> if ($num<100){$num="0".$num;}
>>>> if ($num<10){$num="0".$num;}
>>>
>>> What is the point if this second "if"? You've already covered that with
>>> the first.
>>
>> That serves to add padding to three characters. The last three lines
>> could be replaced with the following:
>>
>> str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);
>
> $num = str_pad(rand(1, 100), 3, '0', STR_PAD_LEFT);
>
> as str_pad() does not modify strings (because they are immutable).
--
Christoph M. Becker]]>Christoph Michael Bec2014-03-14T22:58:25-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185263&th=123442#msg_185263
> On 3/14/2014 11:47 AM, Norman Peelman wrote:
>> On 03/14/2014 11:00 AM, Jerry Stuckle wrote:
>>> On 3/14/2014 9:31 AM, Norman Peelman wrote:
>>>> On 03/12/2014 11:11 PM, Jerry Stuckle wrote:
>>>> > On 3/12/2014 11:02 PM, Norman Peelman wrote:
>>>> >> On 03/12/2014 01:18 PM, richard wrote:
>>>> >>> $x=1;
>>>> >>> while ($x<40){
>>>> >>> $yr=range(60,69);
>>>> >>> shuffle($yr);
>>>> >>> $ayr[$x]=$yr[$x];
>>>> >>> echo $ayr[$x]."<br>";
>>>> >>> $x++;
>>>> >>> }
>>>> >>>
>>>> >>>
>>>> >>> I am attempting to load an array based upon the range of numbers 60
>>>> >>> through
>>>> >>> 69.
>>>> >>> The array will have 40 items.
>>>> >>> This code produces 9 items just fine.
>>>> >>> The rest of the array is empty.
>>>> >>> What am I missing?
>>>> >>>
>>>> >>
>>>> >> for ($items=1; $items<=40; $items++)
>>>> >> {
>>>> >> $ayr[$items] = range(60,69);
>>>> >> shuffle(&$ayr[$items]);
>>>> >> }
>>>> >>
>>>> >>
>>>> >
>>>> > Why would you shuffle a single item?
>>>> >
>>>> >
>>>>
>>>> $ayr[$items] is an Array
>>>>
>>>> php > print_r($ayr);
>>>> Array
>>>> (
>>>> [1] => Array
>>>> (
>>>> [0] => 60
>>>> [1] => 61
>>>> [2] => 62
>>>> [3] => 63
>>>> [4] => 64
>>>> [5] => 65
>>>> [6] => 66
>>>> [7] => 67
>>>> [8] => 68
>>>> [9] => 69
>>>> )
>>>>
>>>> )
>>>>
>>>> ... after Shuffle:
>>>>
>>>> php > shuffle(&$ayr[1]);
>>>> php > print_r($ayr[1]);
>>>> Array
>>>> (
>>>> [0] => 67
>>>> [1] => 69
>>>> [2] => 66
>>>> [3] => 61
>>>> [4] => 62
>>>> [5] => 60
>>>> [6] => 64
>>>> [7] => 68
>>>> [8] => 63
>>>> [9] => 65
>>>> )
>>>>
>>>>
>>>>
>>>
>>> Sorry, I was in a hurry and wasn't paying as much attention.
>>>
>>> But then why have 40 different arrays of 10 items each, anyway?
>>>
>>
>> I don't even know why there are 40 items when you are pulling from a
>> selection of 10 - there's going to be duplicates. Maybe that doesn't
>> matter... I think someone else actually had what Richard was really
>> after.
>>
>>
>>
>
> But each item in your list is an array. He wants single values.
>
>
Now that I checked his page I see what he was after.
--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-]]>Norman Peelman2014-03-15T03:10:27-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185265&th=123442#msg_185265
PointedEars@web.de> writes:
> Daniel Pitts wrote:
>
>> On 3/12/14 10:18 AM, richard wrote:
>>> $x=1;
>>> while ($x<40){
>>> $yr=range(60,69);
>>> shuffle($yr);
>>> $ayr[$x]=$yr[$x];
>>> echo $ayr[$x]."<br>";
>>> $x++;
>>> }
>>>
>>> I am attempting to load an array based upon the range of numbers 60
>>> through 69.
>>> The array will have 40 items.
>>> This code produces 9 items just fine.
>>> The rest of the array is empty.
>>> What am I missing?
>>
>> 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
--
Ben.]]>Ben Bacarisse2014-03-15T21:32:16-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185266&th=123442#msg_185266
> Thomas 'PointedEars' Lahn <PointedEars@web.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
> 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@news.demon.co.uk>]]>Thomas 'PointedEars' 2014-03-15T22:14:17-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185267&th=123442#msg_185267
> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>
>> Daniel Pitts wrote:
>>
>>> On 3/12/14 10:18 AM, richard wrote:
>>>> $x=1;
>>>> while ($x<40){
>>>> $yr=range(60,69);
>>>> shuffle($yr);
>>>> $ayr[$x]=$yr[$x];
>>>> echo $ayr[$x]."<br>";
>>>> $x++;
>>>> }
>>>>
>>>> I am attempting to load an array based upon the range of numbers 60
>>>> through 69.
>>>> The array will have 40 items.
>>>> This code produces 9 items just fine.
>>>> The rest of the array is empty.
>>>> What am I missing?
>>>
>>> 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.
It's generally better to code for clarity first.
--
==================
Remove the "x" from my email address
Jerry Stuckle jstucklex@attglobal.net
==================]]>Jerry Stuckle2014-03-16T02:14:06-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185268&th=123442#msg_185268
jstucklex@attglobal.net> writes:
> On 3/15/2014 5:32 PM, Ben Bacarisse wrote:
>> Thomas 'PointedEars' Lahn <PointedEars@web.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.
> 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.
--
Ben.]]>Ben Bacarisse2014-03-16T02:51:22-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185270&th=123442#msg_185270
> Jerry Stuckle <jstucklex@attglobal.net> writes:
>
>> On 3/15/2014 5:32 PM, Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars@web.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@attglobal.net
==================]]>Jerry Stuckle2014-03-16T13:46:45-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185273&th=123442#msg_185273
> $x=1;
> while ($x<40){
> $yr=range(60,69);
> shuffle($yr);
> $ayr[$x]=$yr[$x];
> echo $ayr[$x]."<br>";
> $x++;
> }
>
>
> I am attempting to load an array based upon the range of numbers 60 through
> 69.
> The array will have 40 items.
> This code produces 9 items just fine.
> The rest of the array is empty.
> What am I missing?
>
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]]>Gabriel2014-03-17T12:43:40-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185305&th=123442#msg_185305
> 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@attglobal.net
==================]]>Jerry Stuckle2014-03-17T12:52:56-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185306&th=123442#msg_185306
^^^^
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16]]>Thomas 'PointedEars' 2014-03-17T13:32:57-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185307&th=123442#msg_185307
> 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:
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]]>Gabriel2014-03-17T13:47:16-00:00Re: Filling an array with random input doesn't quite work
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=185311&th=123442#msg_185311
> On 2014-03-17 13:32:57 +0000, Thomas 'PointedEars' Lahn said:
>> Gabe wrote:
>> ^^^^
>>> 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
It is impolite to full-quote a posting, particularly to quote the signature
of a posting without referring to it. Learn to quote.
> Dear PointedEars
You are confusing this with private e-mail.
> I am so sorry - I sometimes assume that people (on a PHP dev mailing
> list)
This is not a mailing list, it is a Usenet newsgroup. But it makes no
significant difference in that regard:
> would be able to make very basic adjustments to snippets that I
> posted to accommodate for discrepencies.
You have posted *twice* now something that fails to address the problem at
hand. You have been notified of that twice now. You have posted a
correction that is suboptimal, and you have been notified of that. And you
of all people dare to patronize others? Go away.
> I take it you are not one and have not programmed PHP before and are just
> looking to get started.
I have more than 14 years of professional experience in software development
with PHP now.
> Allow me to show you how to make such a change in order to produce an
> array index 1 to 40 inclusive… […]
If you cared to read, *I* know how it is done.
Score adjusted
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann]]>Thomas 'PointedEars' 2014-03-17T15:07:08-00:00