Re: Re: Pre/Post inc (Was array conversion)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 20 Feb 2010 17:44:00 -0500, you wrote:

>On Sat, Feb 20, 2010 at 11:10 AM, Nathan Rixham <nrixham@xxxxxxxxx> wrote:
>
>> Richard Quadling wrote:
>> > On 20 February 2010 11:18,  <clancy_1@xxxxxxxxxxxx> wrote:
>> >> Or:
>> >>
>> >> $a = array ('Cats', 'white', 'Dogs', 'black', 'Mice', 'grey', 'Camels',
>> 'brown');
>> >> $b = '';                                // Just in case it has some
>> leftover value
>> >> $k = 2* (int) (count ($a)/2);   // ensure even no of terms
>> >> $i = 0; while ($i < $k)
>> >>        {
>> >>        $b[$a[$i++]] = $a[$i++];  // ***
>> >>        }
>> >>
>> >> And this works:
>> >> $i = 0; $k = array_keys($b);
>> >> while ($i < count($b)) {        echo '<h5>'.$i.': '.$k[$i].' = '.
>> $b[$k[$i++]].'</h5>'; }
>> >>
>> >> 0: Cats = white
>> >> 1: Dogs = black
>> >> 2: Mice = grey
>> >> 3: Camels = brown
>> >>
>> >> ( *** I have always been wary of using statements like this because I
>> was unsure when the
>> >> incrementing would occur, so I tried it.)
>> >>
>> >> Clancy
>> >
>> >
>> > <?php
>> > $i = 10;
>> > echo $i++; // Shows 10 and $i becomes 11
>> > echo ++$i; // $i becomes 12 and 12 is shown.
>> > ?>
>> >
>> > Post increment and pre increment.
>> >
>> > No need to be "wary" of them.
>> >
>> > http://docs.php.net/manual/en/language.operators.increment.php
>> >
>>
>> Expanding on what Richard says; there does seem to be a growing number
>> of people who haven't stopped to learn the very basics of PHP (or
>> languages in general).
>>
>> I'd strongly recommend that all those in doubt over the basics take a
>> few hours out to (re-)familiarise themselves; and there's no finer
>> resource to do this than the php manual [1]
>>
>> You'll notice the manual goes as follows:
>> # Basic syntax
>> # Types
>> # Variables
>> # Constants
>> # Expressions
>> # Operators
>> # Control Structures
>> # Functions
>> ... more
>>
>> And that's the order in which you should learn; in short you can't
>> really program or script without knowing basics through control structures.
>>
>> Do hope this mail doesn't sound condescending in any way; as it's meant
>> with the best intentions and really will make you're (working) life a
>> lot easier. I myself still refer back to these base sections
>> periodically, and every time I do - a new detail pops out that makes
>> something easier.
>>
>> [1] http://docs.php.net/manual/en/langref.php
>>
>> Many Regards,
>>
>> Nathan
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>Clarifying what Clancy specifically said:
>
>>> ( *** I have always been wary of using statements like this because I was
>unsure when the
>>> incrementing would occur, so I tried it.)
>
>The asterisks inform us he was speaking about the first example, and the
>language informs us he was speaking about the statement, not the increment
>operator, itself.
>
>>> $b[$a[$i++]] = $a[$i++];  // ***
>
>It's actually quite a clever implementation of the algorithm request that
>prompted this thread, and because of it's uniqueness, (incrementing a
>variable multiple times within the same statement on both sides of the '='),
>I don't this reflects that Clancy "stopped to learn the very basics of PHP."
> Actually, Clancy taking the time to try something to better learn the
>language (just for the sake of coding fun, no less) reflects the desire to
>learn new things, and that's exactly the type of person I hope is drawn to
>the PHP community.
>
>Thanks for the example, Clancy :)
>
>Adam

Thanks, Adam.

I started programming before there were any manuals (or classes), and I had to jump in at
the deep end. I do like to know how things work, but the days when you could make your own
complete commented disassembly of the operating system (as I did for CP/M) are long since
gone, and it is no longer possible for any single person to have even a reasonable working
knowledge of how all the programs on his computer operate. I regret I have to admit that
it is now beyond me to have a working familiarity even with the programs I need for
webpage design -- PHP, HTML, CSS and  Javascript, let alone trying to throw in C++, Apache
& Unix.  Also questions like this tend to come up when I'm in the middle of implementing
something complicated, so usually I take the easy way out, instead of taking the time off
to try to answer the question.

The particular case which prompted my comment was the one where you want to copy part of
one array into the corresponding elements of another array. Should you write:

$i = 0; $j=count($a); while ($i < $j) { $b[$i] = $a[$i++]; }	OR

$i = 0; $j=count($a); while ($i < $j) { $b[$i++] = $a[$i]; }

Surprisingly (to me) the answer is neither. (Assuming $j = 5) the first gives $b[1] =
$a[0], with $b[0] undefined, while the second gives $b[0] = $a[1], with $b[4] undefined. 

Experiment shows that you can either be safe, as I have always done, and write:

$i = 0; $j=count($a); while ($i < $j) { $b[$i] = $a[$i]; ++$i; }

or you can write:

$i = -1; $j=count($a) - 1; while ($i < $j) { $b[++$i] = $a[$i]; }

This seemed moderately logical, but then I found you can also write: 

$i = -1; $j=count($a) - 1; while ($i < $j) { $b[$i] = $a[++$i]; }

I think I will stick to the safe way!


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux