Home > Community > Forums > Digital Implementation > Printing common elements between 2 collections

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 Printing common elements between 2 collections 

Last post Tue, Jan 7 2014 11:07 AM by abhishektheone. 2 replies.
Started by abhishektheone 07 Jan 2014 08:28 AM. Topic has 2 replies and 5009 views
Page 1 of 1 (3 items)
Sort Posts:
  • Tue, Jan 7 2014 8:28 AM

    Printing common elements between 2 collections Reply

    Hi,

     Is there a quick way to print all the common elements between 2 collections. Right now I am doing to usual loop method of comparison. Please let me know if there is an easier/faster way.

     

    Regards, 

    • Post Points: 20
  • Tue, Jan 7 2014 8:43 AM

    • BobD
    • Top 25 Contributor
    • Joined on Fri, Jul 11 2008
    • Chelmsford, MA
    • Posts 247
    • Points 9,325
    Re: Printing common elements between 2 collections Reply

    Not that I know of, though I've had interest in doing the same for TCL lists.

    There's a script called "userListUtilities.tcl" in the gifts dir that might be useful. It contains a proc called "userIntersectLists" that takes 2 lists and finds the common elements. If you converted the collections to lists you could use it (or perhaps modify it for your use?).

    For example - say I have a design with 2 instance: i0 and i1

    encounter 2> set a [get_cells i*]
    0x25 

    encounter 8> set b [get_cells i1]                                        
    0x27 

    Collection a contains i0 and i1, and collection "b" contains just i1:

    encounter 10> get_object_name $a
    i0 i1

    encounter 11> get_object_name $b
    i1 

    Then you could use this proc to interest them (which is probably what you've already written but perhaps a little more fast-running): 

    proc userIntersectLists args {
      set res {}
      foreach element [lindex $args 0] {
        set found 1
        foreach list [lrange $args 1 end] {
          if {[lsearch -exact $list $element] < 0} {
           set found 0; break
          }
        }
      if {$found} {lappend res $element}
      }
      set res

    encounter 9> userIntersectLists [get_object_name $a] [get_object_name $b]
    i1

    We've discussed providing more list manipulation in the tool and this is a good example of where that would be helpful. We have pretty cool capabilities for shape manipulation with dbShape but basic TCL lists are, perhaps ironically, harder to work with at the moment.

    Hope this helps,
    Bob 

    • Post Points: 20
  • Tue, Jan 7 2014 11:07 AM

    Re: Printing common elements between 2 collections Reply
    Thanks Bob, This method is definitely fast. Regards, Abhishek
    • Post Points: 5
Page 1 of 1 (3 items)
Sort Posts:
Started by abhishektheone at 07 Jan 2014 08:28 AM. Topic has 2 replies.