Comparing two array lists

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Comparing two array lists

gpatrick
Maybe this isn't the right place to ask a coding question, but I'll try

I am comparing two string arrays and want to list a difference:

class twoArrays
method main(args=String[]) static public
 arrayA = [String 'config', 'usr', 'var', 'msg']
 say 'arrayA length' arrayA.length
 arrayB = [String 'config', 'usr', 'var']
 say 'arrayB length' arrayB.length

 foundSwitch = boolean --0

 loop i=0 to arrayA.length-1
  loop j=0 to arrayB.length-1
    if arrayA[i] = arrayB[j] then do
      foundSwitch = 1
      say "arrayA element" '"'arrayA[i]'"' "was found in arrayB"
    end
    if foundSwitch = 0 then do
      say "arrayA element" arrayA[i] "was not found in arrayB"
      foundSwitch = 0
    end
  end
 end

The result is:
# java twoArrays
arrayA length 4
arrayB length 3
arrayA element "config" was found in arrayB
arrayA element "usr" was found in arrayB
arrayA element "var" was found in arrayB

But I am unable to get at the end:
arrayA element "msg" was not found in arrayB

Would someone be able to spot what I'm missing?  
Reply | Threaded
Open this post in threaded view
|

Re: Comparing two array lists

christel.u.w.pachl christel.u.w.pachl
 loop i=0 to arrayA.length-1
   foundSwitch = 0
   loop j=0 to arrayB.length-1
     if arrayA[i] = arrayB[j] then do
       foundSwitch = 1
       say "arrayA element" '"'arrayA[i]'"' "was found in arrayB"
       end
     end
   if foundSwitch = 0 then
     say "arrayA element" arrayA[i] "was not found in arrayB"
   end
 loop i=0 to arrayB.length-1
   foundSwitch = 0
   loop j=0 to arrayA.length-1
     if arrayA[i] = arrayB[j] then
       foundSwitch = 1
     end
   if foundSwitch = 0 then
     say "arrayB element" arrayB[i] "was not found in arrayA"
   end

--
Walter Pachl

---- gpatrick <[hidden email]> schrieb:

> Maybe this isn't the right place to ask a coding question, but I'll try
>
> I am comparing two string arrays and want to list a difference:
>
> class twoArrays
> method main(args=String[]) static public
>  arrayA = [String 'config', 'usr', 'var', 'msg']
>  say 'arrayA length' arrayA.length
>  arrayB = [String 'config', 'usr', 'var']
>  say 'arrayB length' arrayB.length
>
>  foundSwitch = boolean --0
>
>  loop i=0 to arrayA.length-1
>   loop j=0 to arrayB.length-1
>     if arrayA[i] = arrayB[j] then do
>       foundSwitch = 1
>       say "arrayA element" '"'arrayA[i]'"' "was found in arrayB"
>     end
>     if foundSwitch = 0 then do
>       say "arrayA element" arrayA[i] "was not found in arrayB"
>       foundSwitch = 0
>     end
>   end
>  end
>
> The result is:
> # java twoArrays
> arrayA length 4
> arrayB length 3
> arrayA element "config" was found in arrayB
> arrayA element "usr" was found in arrayB
> arrayA element "var" was found in arrayB
>
> But I am unable to get at the end:
> arrayA element "msg" was not found in arrayB
>
> Would someone be able to spot what I'm missing?  
>
>
>
>
> --
> View this message in context: http://ibm-netrexx.215625.n3.nabble.com/Comparing-two-array-lists-tp4026783.html
> Sent from the ibm-netrexx mailing list archive at Nabble.com.
>
> _______________________________________________
> Ibm-netrexx mailing list
> [hidden email]
> Online Archive : http://ibm-netrexx.215625.n3.nabble.com/
>

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Comparing two array lists

gpatrick
Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Comparing two array lists

billfen
In reply to this post by christel.u.w.pachl christel.u.w.pachl
Shouldn't the second loop say
      if arrayA[j] = arrayB[i] then
?

On 9/27/2013 4:03 PM, Walter Pachl wrote:

>   loop i=0 to arrayA.length-1
>     foundSwitch = 0
>     loop j=0 to arrayB.length-1
>       if arrayA[i] = arrayB[j] then do
>         foundSwitch = 1
>         say "arrayA element" '"'arrayA[i]'"' "was found in arrayB"
>         end
>       end
>     if foundSwitch = 0 then
>       say "arrayA element" arrayA[i] "was not found in arrayB"
>     end
>   loop i=0 to arrayB.length-1
>     foundSwitch = 0
>     loop j=0 to arrayA.length-1
>       if arrayA[i] = arrayB[j] then
>         foundSwitch = 1
>       end
>     if foundSwitch = 0 then
>       say "arrayB element" arrayB[i] "was not found in arrayA"
>     end
>
> --
> Walter Pachl
>
> ---- gpatrick <[hidden email]> schrieb:
>> Maybe this isn't the right place to ask a coding question, but I'll try
>>
>> I am comparing two string arrays and want to list a difference:
>>
>> class twoArrays
>> method main(args=String[]) static public
>>   arrayA = [String 'config', 'usr', 'var', 'msg']
>>   say 'arrayA length' arrayA.length
>>   arrayB = [String 'config', 'usr', 'var']
>>   say 'arrayB length' arrayB.length
>>
>>   foundSwitch = boolean --0
>>
>>   loop i=0 to arrayA.length-1
>>    loop j=0 to arrayB.length-1
>>      if arrayA[i] = arrayB[j] then do
>>        foundSwitch = 1
>>        say "arrayA element" '"'arrayA[i]'"' "was found in arrayB"
>>      end
>>      if foundSwitch = 0 then do
>>        say "arrayA element" arrayA[i] "was not found in arrayB"
>>        foundSwitch = 0
>>      end
>>    end
>>   end
>>
>> The result is:
>> # java twoArrays
>> arrayA length 4
>> arrayB length 3
>> arrayA element "config" was found in arrayB
>> arrayA element "usr" was found in arrayB
>> arrayA element "var" was found in arrayB
>>
>> But I am unable to get at the end:
>> arrayA element "msg" was not found in arrayB
>>
>> Would someone be able to spot what I'm missing?
>>
>>
>>
>>
>> --
>> View this message in context: http://ibm-netrexx.215625.n3.nabble.com/Comparing-two-array-lists-tp4026783.html
>> Sent from the ibm-netrexx mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> Ibm-netrexx mailing list
>> [hidden email]
>> Online Archive : http://ibm-netrexx.215625.n3.nabble.com/
>>
> _______________________________________________
> Ibm-netrexx mailing list
> [hidden email]
> Online Archive : http://ibm-netrexx.215625.n3.nabble.com/
>
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 2014.0.4142 / Virus Database: 3604/6702 - Release Date: 09/26/13
>
>

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Comparing two array lists

christel.u.w.pachl christel.u.w.pachl
probably ;-(
thanx
Walter Pachl


_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Comparing two array lists

Dave Woodman
In reply to this post by christel.u.w.pachl christel.u.w.pachl
I might be tempted to eliminate the inner loop...

import java.util

class twoArrays
method main(args=String[]) static public  
        arrayA = [String 'config', 'usr', 'var', 'msg']  
        say 'arrayA length' arrayA.length  
        arrayB = [String 'config', 'usr', 'var']  
        say 'arrayB length' arrayB.length

        A = ArrayList(Arrays.asList(arrayB))
       
        loop i = 0 to arrayA.length - 1
                if A.indexOf(arrayA[i]) = -1 then say "arrayA element"
'"'arrayA[i]'"' "was not found in arrayB"
                else say "arrayA element" arrayA[i] "was found in arrayB"
        end

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Comparing two array lists

billfen
In reply to this post by gpatrick
Beat me to it :)

/* NetRexx */

class twoArrays

method main(args=String[]) static public
    arrayA = [String 'config', 'usr', 'var', 'msg']
    listA = ArrayList();
    listA.addAll(Arrays.asList(arrayA)); -- converts array to list

    arrayB = [String 'config', 'usr', 'var', 'only in b'] -- note addition
    listB = ArrayList();
    listB.addAll(Arrays.asList(arrayB));

    say
    say 'arrayA length' arrayA.length", arrayB length" arrayB.length;  say

    loop stringA over listA
        if listB.contains(stringA)
        then say "arrayA element" '"'stringA'"' "was found in arrayB"
    end;  say

    loop stringA over listA
        if \ listB.contains(stringA)
        then say "arrayA element" '"'stringA'"' "was not found in arrayB"
    end;  say

    loop stringB over listB
        if \ listA.contains(stringB)
        then say "arrayB element" '"'stringB'"' "was not found in arrayA"
    end;  say


Original email:
-----------------
From: Dave Woodman [hidden email]
Date: Mon, 30 Sep 2013 19:38:49 +0100
To: [hidden email]
Subject: Re: [Ibm-netrexx] Comparing two array lists


I might be tempted to eliminate the inner loop...

import java.util

class twoArrays
method main(args=String[]) static public  
        arrayA = [String 'config', 'usr', 'var', 'msg']  
        say 'arrayA length' arrayA.length  
        arrayB = [String 'config', 'usr', 'var']  
        say 'arrayB length' arrayB.length

        A = ArrayList(Arrays.asList(arrayB))
       
        loop i = 0 to arrayA.length - 1
                if A.indexOf(arrayA[i]) = -1 then say "arrayA element"
'"'arrayA[i]'"' "was not found in arrayB"
                else say "arrayA element" arrayA[i] "was found in arrayB"
        end

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/


--------------------------------------------------------------------
mail2web.com – What can On Demand Business Solutions do for you?
http://link.mail2web.com/Business/SharePoint


_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Comparing two array lists

Kermit Kiser
In reply to this post by gpatrick
You could also use some of the newer features available in NetRexx 3.02:

---------------------------------------------------------------------------------------
class twoArrays
method main(args=String[]) static public
   arrayA = [String 'config', 'usr', 'var', 'msg']
   say 'arrayA length' arrayA.length
   arrayB = [String 'config', 'usr', 'var']
   say 'arrayB length' arrayB.length

   rexxB="not found".buildmap(arrayB,null)          -- convert arrayB to
a reverse indexed Rexx map
   loop A over arrayA
       if rexxB.exists(A) then say "arrayA element" '"'A'"' "was found
in arrayB at index" rexxB[A]-1
           else say "arrayA element" '"'A'"' "was not found in arrayB"
     end
---------------------------------------------------------------------------------------
--              with the following results:
---------------------------------------------------------------------------------------

arrayA length 4
arrayB length 3
arrayA element "config" was found in arrayB at index 0
arrayA element "usr" was found in arrayB at index 1
arrayA element "var" was found in arrayB at index 2
arrayA element "msg" was not found in arrayB

---------------------------------------------------------------------------------------
On 9/27/2013 12:19 PM, gpatrick wrote:

> Maybe this isn't the right place to ask a coding question, but I'll try
>
> I am comparing two string arrays and want to list a difference:
>
> class twoArrays
> method main(args=String[]) static public
>   arrayA = [String 'config', 'usr', 'var', 'msg']
>   say 'arrayA length' arrayA.length
>   arrayB = [String 'config', 'usr', 'var']
>   say 'arrayB length' arrayB.length
>
>   foundSwitch = boolean --0
>
>   loop i=0 to arrayA.length-1
>    loop j=0 to arrayB.length-1
>      if arrayA[i] = arrayB[j] then do
>        foundSwitch = 1
>        say "arrayA element" '"'arrayA[i]'"' "was found in arrayB"
>      end
>      if foundSwitch = 0 then do
>        say "arrayA element" arrayA[i] "was not found in arrayB"
>        foundSwitch = 0
>      end
>    end
>   end
>
> The result is:
> # java twoArrays
> arrayA length 4
> arrayB length 3
> arrayA element "config" was found in arrayB
> arrayA element "usr" was found in arrayB
> arrayA element "var" was found in arrayB
>
> But I am unable to get at the end:
> arrayA element "msg" was not found in arrayB
>
> Would someone be able to spot what I'm missing?
>
>
>
>
> --
> View this message in context: http://ibm-netrexx.215625.n3.nabble.com/Comparing-two-array-lists-tp4026783.html
> Sent from the ibm-netrexx mailing list archive at Nabble.com.
>
> _______________________________________________
> Ibm-netrexx mailing list
> [hidden email]
> Online Archive : http://ibm-netrexx.215625.n3.nabble.com/
>
>
>

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: Comparing two array lists

Tom Maynard
On 30-Sep-13 14:55, Kermit Kiser wrote:
> You could also use some of the newer features available in NetRexx 3.02

Y'know, I'm beginning to wonder what the execution time differences are
between these various solutions.  Perhaps I'll sift through the archives
and compare them myself.  When/if I do, I'll report my results here, of
course.

Kermit's is the clear winner for concision.  I haven't decided on which
might be the clarity winner, but "Form Follows Function" for me ...
hence my interest in the comparative runtimes.  If it doesn't seem
obvious, that's what code comments are for, right?

Tom.

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/