Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13103 > unrolled thread

Filling an array with random input doesn't quite work

Started byrichard <noreply@example.com>
First post2014-03-12 13:18 -0400
Last post2014-03-17 16:07 +0100
Articles 19 on this page of 39 — 12 participants

Back to article view | Back to comp.lang.php


Contents

  Filling an array with random input doesn't quite work richard <noreply@example.com> - 2014-03-12 13:18 -0400
    Re: Filling an array with random input doesn't quite work Tim Streater <timstreater@greenbee.net> - 2014-03-12 17:36 +0000
    Re: Filling an array with random input doesn't quite work Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-12 19:02 +0000
    Re: Filling an array with random input doesn't quite work Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-12 19:06 +0000
    Re: Filling an array with random input doesn't quite work Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-12 20:34 +0000
      Re: Filling an array with random input doesn't quite work Tim Streater <timstreater@greenbee.net> - 2014-03-12 22:51 +0000
    solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) richard <noreply@example.com> - 2014-03-12 17:08 -0400
      Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) Tim Streater <timstreater@greenbee.net> - 2014-03-12 22:49 +0000
        Re: solved! Re: Filling an array with random input doesn't quite work Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-13 01:11 +0100
          Re: solved! Re: Filling an array with random input doesn't quite work richard <noreply@example.com> - 2014-03-12 22:43 -0400
          Re: solved! Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-14 23:23 +0100
            Re: solved! Re: Filling an array with random input doesn't quite work Christoph Michael Becker <cmbecker69@arcor.de> - 2014-03-14 23:58 +0100
        Re: solved! Re: Filling an array with random input doesn't quite work richard <noreply@example.com> - 2014-03-12 21:14 -0400
        Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-13 02:50 +0000
        Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-13 16:23 +0000
      Re: solved! Re: Filling an array with random input doesn't quite work (was: Filling an array with random input doesn't quite work) Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-03-13 02:53 +0000
    Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-12 23:02 -0400
      Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-12 23:11 -0400
        Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-14 09:31 -0400
          Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-14 11:00 -0400
            Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-14 11:47 -0400
              Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-14 12:55 -0400
                Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-14 23:10 -0400
      Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-13 00:31 -0400
    Re: Filling an array with random input doesn't quite work Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2014-03-14 12:42 -0700
      Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-14 23:07 +0100
        Re: Filling an array with random input doesn't quite work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-15 21:32 +0000
          Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-15 23:14 +0100
          Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-15 22:14 -0400
            Re: Filling an array with random input doesn't quite work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-16 02:51 +0000
              Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-16 09:46 -0400
    Re: Filling an array with random input doesn't quite work Gabriel Dragffy <usenet@moojuice.net> - 2014-03-16 14:45 +0000
      Re: Filling an array with random input doesn't quite work Norman Peelman <npeelman@cfl.rr.com> - 2014-03-16 19:52 -0400
      Re: Filling an array with random input doesn't quite work Denis McMahon <denismfmcmahon@gmail.com> - 2014-03-17 02:16 +0000
        Re: Filling an array with random input doesn't quite work Gabe <usenet@moojuice.net> - 2014-03-17 12:43 +0000
          Re: Filling an array with random input doesn't quite work Jerry Stuckle <jstucklex@attglobal.net> - 2014-03-17 08:52 -0400
          Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-17 14:32 +0100
            Re: Filling an array with random input doesn't quite work Gabe <usenet@moojuice.net> - 2014-03-17 13:47 +0000
              Re: Filling an array with random input doesn't quite work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-03-17 16:07 +0100

Page 2 of 2 — ← Prev page 1 [2]


#13135

FromNorman Peelman <npeelman@cfl.rr.com>
Date2014-03-14 11:47 -0400
Message-ID<lfv8ba$7r4$1@dont-email.me>
In reply to#13134
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.



-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#13136

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-14 12:55 -0400
Message-ID<lfvc9s$8f9$1@dont-email.me>
In reply to#13135
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.


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13154

FromNorman Peelman <npeelman@cfl.rr.com>
Date2014-03-14 23:10 -0400
Message-ID<lg0gb4$bq2$1@dont-email.me>
In reply to#13136
On 03/14/2014 12:55 PM, Jerry Stuckle wrote:
> 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?-

[toc] | [prev] | [next] | [standalone]


#13121

FromNorman Peelman <npeelman@cfl.rr.com>
Date2014-03-13 00:31 -0400
Message-ID<lfrca7$q8a$1@dont-email.me>
In reply to#13117
On 03/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]);
> }
>
>

$yr = range(60,69);
for ($items=1; $items<=40; $items++)
{
    $ayr[$items] = $yr;
    shuffle(&$ayr[$items]);
}

or

$yr = range(60,69);
for ($items=1; $items<=40; $items++)
{
    shuffle(&$yr);
    $ayr[$items] = $yr;
}




-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#13142

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2014-03-14 12:42 -0700
Message-ID<CYIUu.12650$%o4.7530@fx09.iad>
In reply to#13103
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.

[toc] | [prev] | [next] | [standalone]


#13145

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-03-14 23:07 +0100
Message-ID<2311625.YxiY5vK2W4@PointedEars.de>
In reply to#13142
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 :)


PointedEars
-- 
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

[toc] | [prev] | [next] | [standalone]


#13156

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-03-15 21:32 +0000
Message-ID<0.6b9d1a51aaf973162356.20140315213216GMT.8761nf5esf.fsf@bsb.me.uk>
In reply to#13145
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.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#13157

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-03-15 23:14 +0100
Message-ID<4900575.IOLIoYm6Jl@PointedEars.de>
In reply to#13156
Ben Bacarisse wrote:

> 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

  $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@news.demon.co.uk>

[toc] | [prev] | [next] | [standalone]


#13158

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-15 22:14 -0400
Message-ID<lg31d6$ema$1@dont-email.me>
In reply to#13156
On 3/15/2014 5:32 PM, Ben Bacarisse wrote:
> 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
==================

[toc] | [prev] | [next] | [standalone]


#13159

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-03-16 02:51 +0000
Message-ID<0.a23b5131e0d1a80cce9a.20140316025122GMT.87siqi500l.fsf@bsb.me.uk>
In reply to#13158
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.

> 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.

[toc] | [prev] | [next] | [standalone]


#13161

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-16 09:46 -0400
Message-ID<lg4a05$dbe$1@dont-email.me>
In reply to#13159
On 3/15/2014 10:51 PM, Ben Bacarisse wrote:
> 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
==================

[toc] | [prev] | [next] | [standalone]


#13164

FromGabriel Dragffy <usenet@moojuice.net>
Date2014-03-16 14:45 +0000
Message-ID<nOiVu.15756$eq5.3796@fx24.am4>
In reply to#13103
On 12/03/2014 17:18, 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?
>

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
)

[toc] | [prev] | [next] | [standalone]


#13177

FromNorman Peelman <npeelman@cfl.rr.com>
Date2014-03-16 19:52 -0400
Message-ID<lg5dgh$gih$1@dont-email.me>
In reply to#13164
On 03/16/2014 10:45 AM, Gabriel Dragffy wrote:
> On 12/03/2014 17:18, 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?
>>
>
> 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
> )

Nice!

-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#13185

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-03-17 02:16 +0000
Message-ID<lg5lto$743$1@dont-email.me>
In reply to#13164
On Sun, 16 Mar 2014 14:45:42 +0000, Gabriel Dragffy wrote:

> On 12/03/2014 17:18, 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?
>>
>>
> 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
    [33] => http://mroldies.net/audio/1961/61-058.mp3
    [32] => http://mroldies.net/audio/1960/60-026.mp3
    [31] => http://mroldies.net/audio/1964/64-090.mp3
    [30] => http://mroldies.net/audio/1961/61-080.mp3
    [29] => http://mroldies.net/audio/1966/66-059.mp3
    [28] => http://mroldies.net/audio/1963/63-066.mp3
    [27] => http://mroldies.net/audio/1965/65-006.mp3
    [26] => http://mroldies.net/audio/1966/66-050.mp3
    [25] => http://mroldies.net/audio/1969/69-004.mp3
    [24] => http://mroldies.net/audio/1963/63-100.mp3
    [23] => http://mroldies.net/audio/1961/61-071.mp3
    [22] => http://mroldies.net/audio/1969/69-082.mp3
    [21] => http://mroldies.net/audio/1962/62-075.mp3
    [20] => http://mroldies.net/audio/1969/69-080.mp3
    [19] => http://mroldies.net/audio/1963/63-039.mp3
    [18] => http://mroldies.net/audio/1963/63-022.mp3
    [17] => http://mroldies.net/audio/1967/67-096.mp3
    [16] => http://mroldies.net/audio/1961/61-041.mp3
    [15] => http://mroldies.net/audio/1960/60-013.mp3
    [14] => http://mroldies.net/audio/1969/69-012.mp3
    [13] => http://mroldies.net/audio/1967/67-087.mp3
    [12] => http://mroldies.net/audio/1961/61-009.mp3
    [11] => http://mroldies.net/audio/1967/67-044.mp3
    [10] => http://mroldies.net/audio/1960/60-021.mp3
    [9] => http://mroldies.net/audio/1965/65-094.mp3
    [8] => http://mroldies.net/audio/1961/61-071.mp3
    [7] => http://mroldies.net/audio/1967/67-040.mp3
    [6] => http://mroldies.net/audio/1967/67-044.mp3
    [5] => http://mroldies.net/audio/1967/67-081.mp3
    [4] => http://mroldies.net/audio/1962/62-022.mp3
    [3] => http://mroldies.net/audio/1966/66-017.mp3
    [2] => http://mroldies.net/audio/1965/65-005.mp3
    [1] => http://mroldies.net/audio/1963/63-076.mp3
)

(although I suspect that he really wants either indexes 1 .. 40 or 0 .. 
39, but it's hard to tell which he wants from his code.

Hence my solution of:

$x=40;
$song=array();
while(--$x)$song[$x]=sprintf("http://mroldies.net/audio/19%d/%d-%03d.mp3",
$y=rand(60,69),$y,rand(1,100));

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#13193

FromGabe <usenet@moojuice.net>
Date2014-03-17 12:43 +0000
Message-ID<2014031712434095253-usenet@moojuicenet>
In reply to#13185
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

[toc] | [prev] | [next] | [standalone]


#13194

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-03-17 08:52 -0400
Message-ID<lg6r77$hn$1@dont-email.me>
In reply to#13193
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@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13195

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-03-17 14:32 +0100
Message-ID<1701209.hILuKnBK3U@PointedEars.de>
In reply to#13193
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
-- 
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

[toc] | [prev] | [next] | [standalone]


#13196

FromGabe <usenet@moojuice.net>
Date2014-03-17 13:47 +0000
Message-ID<2014031713471617567-usenet@moojuicenet>
In reply to#13195
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

[toc] | [prev] | [next] | [standalone]


#13200

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-03-17 16:07 +0100
Message-ID<2936930.zkYQ50mGB4@PointedEars.de>
In reply to#13196
Gabe wrote:

> 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

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.php


csiph-web