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


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

how to join two arrays?

Started byrichard <noreply@example.com>
First post2014-05-03 13:25 -0400
Last post2014-05-05 13:36 +0000
Articles 4 on this page of 24 — 9 participants

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


Contents

  how to join two arrays? richard <noreply@example.com> - 2014-05-03 13:25 -0400
    Re: how to join two arrays? "J.O. Aho" <user@example.net> - 2014-05-03 22:07 +0200
    Re: how to join two arrays? Denis McMahon <denismfmcmahon@gmail.com> - 2014-05-03 22:23 +0000
      Re: how to join two arrays? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-04 19:56 +0200
        Re: how to join two arrays? Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-04 15:17 -0400
          Re: how to join two arrays? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-04 21:47 +0200
            Re: how to join two arrays? Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-04 16:48 -0400
              Re: how to join two arrays? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-04 23:02 +0200
                Re: how to join two arrays? Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-04 17:51 -0400
                  Re: how to join two arrays? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-05-05 00:55 +0200
                    Re: how to join two arrays? Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-04 21:31 -0400
                      Re: how to join two arrays? Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-04 22:13 -0400
                        Re: how to join two arrays? Richard Yates <richard@yatesguitar.com> - 2014-05-05 12:45 -0700
    solved    (was: how to join two arrays?) richard <noreply@example.com> - 2014-05-04 19:54 -0400
      Re: solved richard <noreply@example.com> - 2014-05-04 22:00 -0400
        Re: solved Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-04 22:27 -0400
          Re: solved richard <noreply@example.com> - 2014-05-04 23:07 -0400
            Re: solved Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-05 11:11 +0100
              Re: solved richard <noreply@example.com> - 2014-05-05 09:42 -0400
                Re: solved Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-05 15:51 +0200
                Re: solved Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-05 10:26 -0400
                  Re: solved richard <noreply@example.com> - 2014-05-05 10:34 -0400
                Re: solved Doug Miller <doug_at_milmac_dot_com@example.com> - 2014-05-06 00:55 +0000
            Re: solved Denis McMahon <denismfmcmahon@gmail.com> - 2014-05-05 13:36 +0000

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


#13520 — Re: solved

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-05-05 10:26 -0400
SubjectRe: solved
Message-ID<lk8736$jdn$1@dont-email.me>
In reply to#13517
On 5/5/2014 9:42 AM, richard wrote:
> On Mon, 05 May 2014 11:11:35 +0100, Ben Bacarisse wrote:
>
>> richard <noreply@example.com> writes:
>>
>>> On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:
>> <snip>
>>>> But you didn't show your entire code, so it's hard to say what could be
>>>> wrong.
>>>
>>> for ($x=0;$x<=$number;$x++){
>>>
>>>    if ($files[x] !=".") {
>>                 ^ not $x
>>
>>>     $master[$lo]=$files[$x];
>>> 	 $lo=$lo+1;
>>>       }
>>> }
>>
>> You really need to find some what to work which lets you see all the
>> notices and warnings that PHP can give you.
>>
>> By the way, writing $master[] = ... puts an element at the next unused
>> numerical index in the array which might be what you are doing here with
>> $lo.  I'd write the above like this:
>>
>>    foreach ($files as $file)
>>         if ($file !== '.')
>>              $master[] = $file;
>
> Thanks. Works like a charm.
> Now how do I also include the ".."?
>    foreach ($files as $file)
>         if ($file !== '.' || $file!=="..")
>              $master[] = $file;
> This does not seem to work.
>
> Time to implement the formidable goto eh?
>

You're close.  You want to add it if $file != "." AND $file != "..".

If $file equals ".", it cannot equal "..", and vice versa.  So the test 
you have is always true.

So what you want is

            if ($file !== "." && $file !== "..")
...


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

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


#13522 — Re: solved

Fromrichard <noreply@example.com>
Date2014-05-05 10:34 -0400
SubjectRe: solved
Message-ID<15udk1p8og28i.43as2vgbnzua.dlg@40tude.net>
In reply to#13520
On Mon, 05 May 2014 10:26:51 -0400, Jerry Stuckle wrote:

> On 5/5/2014 9:42 AM, richard wrote:
>> On Mon, 05 May 2014 11:11:35 +0100, Ben Bacarisse wrote:
>>
>>> richard <noreply@example.com> writes:
>>>
>>>> On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:
>>> <snip>
>>>>> But you didn't show your entire code, so it's hard to say what could be
>>>>> wrong.
>>>>
>>>> for ($x=0;$x<=$number;$x++){
>>>>
>>>>    if ($files[x] !=".") {
>>>                 ^ not $x
>>>
>>>>     $master[$lo]=$files[$x];
>>>> 	 $lo=$lo+1;
>>>>       }
>>>> }
>>>
>>> You really need to find some what to work which lets you see all the
>>> notices and warnings that PHP can give you.
>>>
>>> By the way, writing $master[] = ... puts an element at the next unused
>>> numerical index in the array which might be what you are doing here with
>>> $lo.  I'd write the above like this:
>>>
>>>    foreach ($files as $file)
>>>         if ($file !== '.')
>>>              $master[] = $file;
>>
>> Thanks. Works like a charm.
>> Now how do I also include the ".."?
>>    foreach ($files as $file)
>>         if ($file !== '.' || $file!=="..")
>>              $master[] = $file;
>> This does not seem to work.
>>
>> Time to implement the formidable goto eh?
>>
> 
> You're close.  You want to add it if $file != "." AND $file != "..".
> 
> If $file equals ".", it cannot equal "..", and vice versa.  So the test 
> you have is always true.
> 
> So what you want is
> 
>             if ($file !== "." && $file !== "..")
> ...

thank you sir that worked perfectly.

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


#13536 — Re: solved

FromDoug Miller <doug_at_milmac_dot_com@example.com>
Date2014-05-06 00:55 +0000
SubjectRe: solved
Message-ID<XnsA324D4D6F9E1Adougmilmaccom@78.46.70.116>
In reply to#13517
richard <noreply@example.com> wrote in news:1u66bqszrlp57$.2ce1bbokt163.dlg@
40tude.net:

[...]
>        if ($file !== '.' || $file!=="..")
>             $master[] = $file;
> This does not seem to work.

Depends on your definition of "work", I suppose. It definitely does work, in the sense that it 
certainly is doing what you told it to. I'm pretty sure it's not doing what you expected it to do, 
but that's because your expectations don't match your code.

Consider what happens when $file is equal to "." The first part of your compound condition 
is FALSE. The second part is TRUE.

FALSE || TRUE is TRUE.

Now what happens when $file is equal to "..". The first part is TRUE, and the second part is 
FALSE.

TRUE || FALSE is TRUE.

Finally, if $file is equal to, say, "abc". Now both parts of the condition are TRUE.

TRUE || TRUE is TRUE.

The results you *want* are FALSE, FALSE, TRUE respectively -- that is, you want the result 
to be FALSE when *either* of its inputs is FALSE, and TRUE only if *both* of its inputs are 
TRUE. That describes &&, not ||.

> 
> Time to implement the formidable goto eh?

No, it's time to fix your broken logic. Google "De Morgan's Laws" for more information.

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


#13516 — Re: solved

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-05-05 13:36 +0000
SubjectRe: solved
Message-ID<lk845o$l73$2@dont-email.me>
In reply to#13514
On Sun, 04 May 2014 23:07:55 -0400, richard wrote:

> On Sun, 04 May 2014 22:27:01 -0400, Jerry Stuckle wrote:

>> If written correctly a conditional for !="." would get rid of the '.'
>> entry; !=".." would get rid of the ".." entry.
>> 
>> But you didn't show your entire code, so it's hard to say what could be
>> wrong.

> for ($x=0;$x<=$number;$x++){
>   if ($files[x] !=".") {

As Ben pointed out, missing $ from [$x] in $files[x], but that's just 
your usual broken code.

>    $master[$lo]=$files[$x];
> 	 $lo=$lo+1;
>      }
> }

for ( $x=0; $x <= $number; $x++ )
  if ( $files[$x] != "." && $files[$x] != ".." )
    $master[$lo++] = $files[$x];

The line:

$master[$lo++] = $files[$x];

does the following:

First it assigns the value of $files[$x] to $master[$lo]
Then it increments $lo by 1

And as Ben pointed out, it's possible that the following will work too:

for ( $x=0; $x <= $number; $x++ )
  if ( $files[$x] != "." && $files[$x] != ".." )
    $master[] = $files[$x];

As it just increments the numeric index of $master by 1 each time it 
assigns another element.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


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

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


csiph-web