Pre/Post inc (Was array conversion)

Pre/Post inc (Was array conversion)

am 20.02.2010 12:48:58 von Richard Quadling

On 20 February 2010 11:18, wrote:
> Or:
>
> $a =3D array ('Cats', 'white', 'Dogs', 'black', 'Mice', 'grey', 'Camels',=
'brown');
> $b =3D '';                  =
             // Just in case it has som=
e leftover value
> $k =3D 2* (int) (count ($a)/2);   // ensure even no of terms
> $i =3D 0; while ($i < $k)
>        {
>        $b[$a[$i++]] =3D $a[$i++];  // ***
>        }
>
> And this works:
> $i =3D 0; $k =3D array_keys($b);
> while ($i < count($b)) {        echo '

'.$i.': '.$=
k[$i].' =3D '. $b[$k[$i++]].'
'; }
>
> 0: Cats =3D white
> 1: Dogs =3D black
> 2: Mice =3D grey
> 3: Camels =3D 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


$i =3D 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.p hp

--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

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

Re: Pre/Post inc (Was array conversion)

am 20.02.2010 17:10:32 von Nathan Rixham

Richard Quadling wrote:
> On 20 February 2010 11:18, 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 '

'.$i.': '.$k[$i].' = '. $b[$k[$i++]].'
'; }
>>
>> 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
>
>
> > $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.p hp
>

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

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

am 20.02.2010 23:44:00 von Adam Richardson

--000e0cdfd85ea6bcf704800fefb2
Content-Type: text/plain; charset=ISO-8859-1

On Sat, Feb 20, 2010 at 11:10 AM, Nathan Rixham wrote:

> Richard Quadling wrote:
> > On 20 February 2010 11:18, 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 '

'.$i.': '.$k[$i].' = '.
> $b[$k[$i++]].'
'; }
> >>
> >> 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
> >
> >
> > > > $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.p hp
> >
>
> 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

--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com

--000e0cdfd85ea6bcf704800fefb2--

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

am 21.02.2010 02:38:00 von Clancy

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

>On Sat, Feb 20, 2010 at 11:10 AM, Nathan Rixham =
wrote:
>
>> Richard Quadling wrote:
>> > On 20 February 2010 11:18, wrote:
>> >> Or:
>> >>
>> >> $a =3D array ('Cats', 'white', 'Dogs', 'black', 'Mice', 'grey', =
'Camels',
>> 'brown');
>> >> $b =3D ''; // Just in case it has =
some
>> leftover value
>> >> $k =3D 2* (int) (count ($a)/2); // ensure even no of terms
>> >> $i =3D 0; while ($i < $k)
>> >> {
>> >> $b[$a[$i++]] =3D $a[$i++]; // ***
>> >> }
>> >>
>> >> And this works:
>> >> $i =3D 0; $k =3D array_keys($b);
>> >> while ($i < count($b)) { echo '

'.$i.': '.$k[$i].' =3D '.
>> $b[$k[$i++]].'
'; }
>> >>
>> >> 0: Cats =3D white
>> >> 1: Dogs =3D black
>> >> 2: Mice =3D grey
>> >> 3: Camels =3D 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
>> >
>> >
>> > >> > $i =3D 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.p hp
>> >
>>
>> 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++]] =3D $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 =
'=3D'),
>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 =3D 0; $j=3Dcount($a); while ($i < $j) { $b[$i] =3D $a[$i++]; } OR

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

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

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

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

or you can write:

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

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

$i =3D -1; $j=3Dcount($a) - 1; while ($i < $j) { $b[$i] =3D $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