compare two list and display the different elemts

Altair Forum User
Altair Forum User
Altair Employee
edited October 2020 in Community Q&A

hi all,

set a '1 2 3 '

set b ' 4 1 3 8 2'

 i need to compare these two lists ...not on the basis of order of arrangement only difference 

output i need is  4  8

 

i tried using using *markintersection its not working

Answers

  • tinh
    tinh Altair Community Member
    edited November 2017

    Hi

    *markintersection is used for hm entities only.

    a proc like this

    proc ::ldiff {list1 list2} {

         set result {}

         foreach item $list1 {if {[lsearch $list2 $item]==-1} {lappend result $item}}

         set result

    }

    #now call

    ldiff $b $a

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2017

     

    hi tinh ,

    ya this is working....in case if we have

    set a '1 2 3'

    set b '1 3 4'

    out put

    2 4

    in this case we have to loop both the lists and append in a separate list . ryt?

     

  • tinh
    tinh Altair Community Member
    edited November 2017

    just try it and show us your results

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2017

    just try it and show us your results

    chào Tĩnh, mình mới học về hypermesh và đang muốn viết script cho hypermesh, bạn học về hypermesh à :D/emoticons/default_biggrin.png' srcset='/emoticons/biggrin@2x.png 2x' title=':D' width='20' />

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited November 2017

    hi  tinh,

    in the code which u have written ,what does set result does.?(after the for loop)

     

  • tinh
    tinh Altair Community Member
    edited November 2017

    it returns result.

    i think it is not neccessary. because lappend will return it

    could you remove it and try?

  • tinh
    tinh Altair Community Member
    edited November 2017

    no no, it is needed. because sometimes lsearch not found item, it will return emptied string