Compare 'n' number of lists in TclTCL How to read , extract and count the occurent in .txt file (Current Directory)Floating number comparing in tclSearching for a number in a sorted list in TclTCL Program that Compare StringSearching from list in tclWhy can't I use `w` for `while` in Tcl's interactive mode?How can I compare two lists (containing character and number in it) in tcl?Compare two paths in tclTCL Group list number by ruleGetting an element from a list in Tcl at a particular index for a given condition

Start making guitar arrangements

What is this cable/device?

Why electric field inside a cavity of a non-conducting sphere not zero?

How much character growth crosses the line into breaking the character

Should I outline or discovery write my stories?

If infinitesimal transformations commute why dont the generators of the Lorentz group commute?

Multiplicative persistence

Why is it that I can sometimes guess the next note?

Not using 's' for he/she/it

How to bake one texture for one mesh with multiple textures blender 2.8

Redundant comparison & "if" before assignment

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?

Can I sign legal documents with a smiley face?

How to explain what's wrong with this application of the chain rule?

Why should universal income be universal?

What is Cash Advance APR?

How to implement a feedback to keep the DC gain at zero for this conceptual passive filter?

Must Legal Documents Be Siged In Standard Pen Colors?

dpdt switch to spst switch

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

Store Credit Card Information in Password Manager?

How should I respond when I lied about my education and the company finds out through background check?

Where does the bonus feat in the cleric starting package come from?



Compare 'n' number of lists in Tcl


TCL How to read , extract and count the occurent in .txt file (Current Directory)Floating number comparing in tclSearching for a number in a sorted list in TclTCL Program that Compare StringSearching from list in tclWhy can't I use `w` for `while` in Tcl's interactive mode?How can I compare two lists (containing character and number in it) in tcl?Compare two paths in tclTCL Group list number by ruleGetting an element from a list in Tcl at a particular index for a given condition













1















I have the following lists:



set w 1 2 3 4 5 6 7
set x 1 2 3 4 5 8 9
set y 1 2 3 4 0 9 1
set z 1 2 3 4 5 6 7


I want to compare all the lists - w.r.t corresponding indices - and find out the common elements and append those common elements to a new list. If I compare the above lists, I see that 1 2 3 4 are common across all the lists and have the same index, so my output should be:



1 2 3 4


If there are no common elements(even at 0'th index) my new list will be empty.



I first start off by creating a list of lists:



set l1 [list $w $x $y $z]


Then I create a nested loop to compare the lists and extract my common elements, I will be using list 'x' as my reference list:



for set j 0 $j < [llength $x] incr j 
for set i 1 $i < [llength $l1] incr i

set a [lindex [lindex $l1 $i] $j]

if $a == [lindex $x $j] && [lindex $l2 $j] ==
lappend l2 $a
else
break






What I'm getting is:



1 2 3 4 5









share|improve this question


























    1















    I have the following lists:



    set w 1 2 3 4 5 6 7
    set x 1 2 3 4 5 8 9
    set y 1 2 3 4 0 9 1
    set z 1 2 3 4 5 6 7


    I want to compare all the lists - w.r.t corresponding indices - and find out the common elements and append those common elements to a new list. If I compare the above lists, I see that 1 2 3 4 are common across all the lists and have the same index, so my output should be:



    1 2 3 4


    If there are no common elements(even at 0'th index) my new list will be empty.



    I first start off by creating a list of lists:



    set l1 [list $w $x $y $z]


    Then I create a nested loop to compare the lists and extract my common elements, I will be using list 'x' as my reference list:



    for set j 0 $j < [llength $x] incr j 
    for set i 1 $i < [llength $l1] incr i

    set a [lindex [lindex $l1 $i] $j]

    if $a == [lindex $x $j] && [lindex $l2 $j] ==
    lappend l2 $a
    else
    break






    What I'm getting is:



    1 2 3 4 5









    share|improve this question
























      1












      1








      1








      I have the following lists:



      set w 1 2 3 4 5 6 7
      set x 1 2 3 4 5 8 9
      set y 1 2 3 4 0 9 1
      set z 1 2 3 4 5 6 7


      I want to compare all the lists - w.r.t corresponding indices - and find out the common elements and append those common elements to a new list. If I compare the above lists, I see that 1 2 3 4 are common across all the lists and have the same index, so my output should be:



      1 2 3 4


      If there are no common elements(even at 0'th index) my new list will be empty.



      I first start off by creating a list of lists:



      set l1 [list $w $x $y $z]


      Then I create a nested loop to compare the lists and extract my common elements, I will be using list 'x' as my reference list:



      for set j 0 $j < [llength $x] incr j 
      for set i 1 $i < [llength $l1] incr i

      set a [lindex [lindex $l1 $i] $j]

      if $a == [lindex $x $j] && [lindex $l2 $j] ==
      lappend l2 $a
      else
      break






      What I'm getting is:



      1 2 3 4 5









      share|improve this question














      I have the following lists:



      set w 1 2 3 4 5 6 7
      set x 1 2 3 4 5 8 9
      set y 1 2 3 4 0 9 1
      set z 1 2 3 4 5 6 7


      I want to compare all the lists - w.r.t corresponding indices - and find out the common elements and append those common elements to a new list. If I compare the above lists, I see that 1 2 3 4 are common across all the lists and have the same index, so my output should be:



      1 2 3 4


      If there are no common elements(even at 0'th index) my new list will be empty.



      I first start off by creating a list of lists:



      set l1 [list $w $x $y $z]


      Then I create a nested loop to compare the lists and extract my common elements, I will be using list 'x' as my reference list:



      for set j 0 $j < [llength $x] incr j 
      for set i 1 $i < [llength $l1] incr i

      set a [lindex [lindex $l1 $i] $j]

      if $a == [lindex $x $j] && [lindex $l2 $j] ==
      lappend l2 $a
      else
      break






      What I'm getting is:



      1 2 3 4 5






      tcl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 7:40









      LowerMoonLowerMoon

      315




      315






















          2 Answers
          2






          active

          oldest

          votes


















          1














          You are effectively only comparing list x against list x, and the actual output from your code above (assuming list l2 is initially empty) is actually:



          1 2 3 4 5 8 9


          You might ask:




          Why is it comparing list x against list x?




          Your inner loop starts at index 1 (set i 1), which is list x in l1.



          You might further ask:




          Why are the other lists not being compared?




          Once you have appended something to l2, lindex $l2 $j for the next list will never be empty, so the inner loop will break.




          So, how to do it?



          I would probably use something like this:



          set w 1 2 3 4 5 6 7
          set x 1 2 3 4 5 8 9
          set y 1 2 3 4 0 9 1
          set z 1 2 3 4 5 6 7

          set l1 [list $w $x $y $z]
          set l2 [list]

          set num [llength $x]

          for set i 0 $i < $num incr i
          # This variable will tell us how many matched. 0 indicating none.
          set same 0

          for set j 0 $j < [llength $l1] incr j
          # lindex accepts multiple indices, see the manual
          # using x as reference, if the current list's ith element is the same as x's ith element...
          if [lindex $l1 $j $i] == [lindex $x $i]
          incr same



          # if same reached 4, means 4 matched
          if $same == 4
          lappend l2 [lindex $x $i]




          Result:



          1 2 3 4



          You could make the inner loop break if the elements do not match, so it won't loop unnecessarily.



          Or instead of counting the number of matches, you could check if the inner loop broke with something like:



          for set i 0 $i < $num incr i 
          set broke 0

          for set j 0 $j < [llength $l1] incr j
          if [lindex $l1 $j $i] != [lindex $x $i]
          set broke 1
          break



          # if it did not break, then we know all matched
          if $broke == 0
          lappend l2 [lindex $x $i]







          share|improve this answer























          • Thanks, this works! I didn't notice that blunder of comparing against the same list, my initial goal was to avoid it and keep the first list in l1 (or) w as my reference and skip it during the comparison. Also, the reason I added && [lindex $l2 $j] == was because I was getting 1 1 1 2 2 2 3 3 3 4 4 4 5. I'm still a bit unsure on why my loop isn't terminating when a becomes 5.

            – LowerMoon
            Mar 7 at 8:36












          • Never mind, I understood. My loop just appends the element even if the element matches the element in the second list. I didn't add code to delete the already appended number if it finds an inequality in the other n-1 lists.

            – LowerMoon
            Mar 7 at 8:47


















          2














          Similar implementation, but using an array to store the unique elements of the slice



          set lists [list $w $x $y $z]
          set common [list]

          for set i 0 $i < [llength $w] incr i
          array unset elements
          foreach list $lists
          set elements([lindex $list $i]) dummyvalue

          set unique [array names elements]
          if [llength $unique] == 1
          lappend common $unique



          puts $common ;# => 1 2 3 4





          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038457%2fcompare-n-number-of-lists-in-tcl%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You are effectively only comparing list x against list x, and the actual output from your code above (assuming list l2 is initially empty) is actually:



            1 2 3 4 5 8 9


            You might ask:




            Why is it comparing list x against list x?




            Your inner loop starts at index 1 (set i 1), which is list x in l1.



            You might further ask:




            Why are the other lists not being compared?




            Once you have appended something to l2, lindex $l2 $j for the next list will never be empty, so the inner loop will break.




            So, how to do it?



            I would probably use something like this:



            set w 1 2 3 4 5 6 7
            set x 1 2 3 4 5 8 9
            set y 1 2 3 4 0 9 1
            set z 1 2 3 4 5 6 7

            set l1 [list $w $x $y $z]
            set l2 [list]

            set num [llength $x]

            for set i 0 $i < $num incr i
            # This variable will tell us how many matched. 0 indicating none.
            set same 0

            for set j 0 $j < [llength $l1] incr j
            # lindex accepts multiple indices, see the manual
            # using x as reference, if the current list's ith element is the same as x's ith element...
            if [lindex $l1 $j $i] == [lindex $x $i]
            incr same



            # if same reached 4, means 4 matched
            if $same == 4
            lappend l2 [lindex $x $i]




            Result:



            1 2 3 4



            You could make the inner loop break if the elements do not match, so it won't loop unnecessarily.



            Or instead of counting the number of matches, you could check if the inner loop broke with something like:



            for set i 0 $i < $num incr i 
            set broke 0

            for set j 0 $j < [llength $l1] incr j
            if [lindex $l1 $j $i] != [lindex $x $i]
            set broke 1
            break



            # if it did not break, then we know all matched
            if $broke == 0
            lappend l2 [lindex $x $i]







            share|improve this answer























            • Thanks, this works! I didn't notice that blunder of comparing against the same list, my initial goal was to avoid it and keep the first list in l1 (or) w as my reference and skip it during the comparison. Also, the reason I added && [lindex $l2 $j] == was because I was getting 1 1 1 2 2 2 3 3 3 4 4 4 5. I'm still a bit unsure on why my loop isn't terminating when a becomes 5.

              – LowerMoon
              Mar 7 at 8:36












            • Never mind, I understood. My loop just appends the element even if the element matches the element in the second list. I didn't add code to delete the already appended number if it finds an inequality in the other n-1 lists.

              – LowerMoon
              Mar 7 at 8:47















            1














            You are effectively only comparing list x against list x, and the actual output from your code above (assuming list l2 is initially empty) is actually:



            1 2 3 4 5 8 9


            You might ask:




            Why is it comparing list x against list x?




            Your inner loop starts at index 1 (set i 1), which is list x in l1.



            You might further ask:




            Why are the other lists not being compared?




            Once you have appended something to l2, lindex $l2 $j for the next list will never be empty, so the inner loop will break.




            So, how to do it?



            I would probably use something like this:



            set w 1 2 3 4 5 6 7
            set x 1 2 3 4 5 8 9
            set y 1 2 3 4 0 9 1
            set z 1 2 3 4 5 6 7

            set l1 [list $w $x $y $z]
            set l2 [list]

            set num [llength $x]

            for set i 0 $i < $num incr i
            # This variable will tell us how many matched. 0 indicating none.
            set same 0

            for set j 0 $j < [llength $l1] incr j
            # lindex accepts multiple indices, see the manual
            # using x as reference, if the current list's ith element is the same as x's ith element...
            if [lindex $l1 $j $i] == [lindex $x $i]
            incr same



            # if same reached 4, means 4 matched
            if $same == 4
            lappend l2 [lindex $x $i]




            Result:



            1 2 3 4



            You could make the inner loop break if the elements do not match, so it won't loop unnecessarily.



            Or instead of counting the number of matches, you could check if the inner loop broke with something like:



            for set i 0 $i < $num incr i 
            set broke 0

            for set j 0 $j < [llength $l1] incr j
            if [lindex $l1 $j $i] != [lindex $x $i]
            set broke 1
            break



            # if it did not break, then we know all matched
            if $broke == 0
            lappend l2 [lindex $x $i]







            share|improve this answer























            • Thanks, this works! I didn't notice that blunder of comparing against the same list, my initial goal was to avoid it and keep the first list in l1 (or) w as my reference and skip it during the comparison. Also, the reason I added && [lindex $l2 $j] == was because I was getting 1 1 1 2 2 2 3 3 3 4 4 4 5. I'm still a bit unsure on why my loop isn't terminating when a becomes 5.

              – LowerMoon
              Mar 7 at 8:36












            • Never mind, I understood. My loop just appends the element even if the element matches the element in the second list. I didn't add code to delete the already appended number if it finds an inequality in the other n-1 lists.

              – LowerMoon
              Mar 7 at 8:47













            1












            1








            1







            You are effectively only comparing list x against list x, and the actual output from your code above (assuming list l2 is initially empty) is actually:



            1 2 3 4 5 8 9


            You might ask:




            Why is it comparing list x against list x?




            Your inner loop starts at index 1 (set i 1), which is list x in l1.



            You might further ask:




            Why are the other lists not being compared?




            Once you have appended something to l2, lindex $l2 $j for the next list will never be empty, so the inner loop will break.




            So, how to do it?



            I would probably use something like this:



            set w 1 2 3 4 5 6 7
            set x 1 2 3 4 5 8 9
            set y 1 2 3 4 0 9 1
            set z 1 2 3 4 5 6 7

            set l1 [list $w $x $y $z]
            set l2 [list]

            set num [llength $x]

            for set i 0 $i < $num incr i
            # This variable will tell us how many matched. 0 indicating none.
            set same 0

            for set j 0 $j < [llength $l1] incr j
            # lindex accepts multiple indices, see the manual
            # using x as reference, if the current list's ith element is the same as x's ith element...
            if [lindex $l1 $j $i] == [lindex $x $i]
            incr same



            # if same reached 4, means 4 matched
            if $same == 4
            lappend l2 [lindex $x $i]




            Result:



            1 2 3 4



            You could make the inner loop break if the elements do not match, so it won't loop unnecessarily.



            Or instead of counting the number of matches, you could check if the inner loop broke with something like:



            for set i 0 $i < $num incr i 
            set broke 0

            for set j 0 $j < [llength $l1] incr j
            if [lindex $l1 $j $i] != [lindex $x $i]
            set broke 1
            break



            # if it did not break, then we know all matched
            if $broke == 0
            lappend l2 [lindex $x $i]







            share|improve this answer













            You are effectively only comparing list x against list x, and the actual output from your code above (assuming list l2 is initially empty) is actually:



            1 2 3 4 5 8 9


            You might ask:




            Why is it comparing list x against list x?




            Your inner loop starts at index 1 (set i 1), which is list x in l1.



            You might further ask:




            Why are the other lists not being compared?




            Once you have appended something to l2, lindex $l2 $j for the next list will never be empty, so the inner loop will break.




            So, how to do it?



            I would probably use something like this:



            set w 1 2 3 4 5 6 7
            set x 1 2 3 4 5 8 9
            set y 1 2 3 4 0 9 1
            set z 1 2 3 4 5 6 7

            set l1 [list $w $x $y $z]
            set l2 [list]

            set num [llength $x]

            for set i 0 $i < $num incr i
            # This variable will tell us how many matched. 0 indicating none.
            set same 0

            for set j 0 $j < [llength $l1] incr j
            # lindex accepts multiple indices, see the manual
            # using x as reference, if the current list's ith element is the same as x's ith element...
            if [lindex $l1 $j $i] == [lindex $x $i]
            incr same



            # if same reached 4, means 4 matched
            if $same == 4
            lappend l2 [lindex $x $i]




            Result:



            1 2 3 4



            You could make the inner loop break if the elements do not match, so it won't loop unnecessarily.



            Or instead of counting the number of matches, you could check if the inner loop broke with something like:



            for set i 0 $i < $num incr i 
            set broke 0

            for set j 0 $j < [llength $l1] incr j
            if [lindex $l1 $j $i] != [lindex $x $i]
            set broke 1
            break



            # if it did not break, then we know all matched
            if $broke == 0
            lappend l2 [lindex $x $i]








            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 7 at 8:02









            JerryJerry

            59.8k1170106




            59.8k1170106












            • Thanks, this works! I didn't notice that blunder of comparing against the same list, my initial goal was to avoid it and keep the first list in l1 (or) w as my reference and skip it during the comparison. Also, the reason I added && [lindex $l2 $j] == was because I was getting 1 1 1 2 2 2 3 3 3 4 4 4 5. I'm still a bit unsure on why my loop isn't terminating when a becomes 5.

              – LowerMoon
              Mar 7 at 8:36












            • Never mind, I understood. My loop just appends the element even if the element matches the element in the second list. I didn't add code to delete the already appended number if it finds an inequality in the other n-1 lists.

              – LowerMoon
              Mar 7 at 8:47

















            • Thanks, this works! I didn't notice that blunder of comparing against the same list, my initial goal was to avoid it and keep the first list in l1 (or) w as my reference and skip it during the comparison. Also, the reason I added && [lindex $l2 $j] == was because I was getting 1 1 1 2 2 2 3 3 3 4 4 4 5. I'm still a bit unsure on why my loop isn't terminating when a becomes 5.

              – LowerMoon
              Mar 7 at 8:36












            • Never mind, I understood. My loop just appends the element even if the element matches the element in the second list. I didn't add code to delete the already appended number if it finds an inequality in the other n-1 lists.

              – LowerMoon
              Mar 7 at 8:47
















            Thanks, this works! I didn't notice that blunder of comparing against the same list, my initial goal was to avoid it and keep the first list in l1 (or) w as my reference and skip it during the comparison. Also, the reason I added && [lindex $l2 $j] == was because I was getting 1 1 1 2 2 2 3 3 3 4 4 4 5. I'm still a bit unsure on why my loop isn't terminating when a becomes 5.

            – LowerMoon
            Mar 7 at 8:36






            Thanks, this works! I didn't notice that blunder of comparing against the same list, my initial goal was to avoid it and keep the first list in l1 (or) w as my reference and skip it during the comparison. Also, the reason I added && [lindex $l2 $j] == was because I was getting 1 1 1 2 2 2 3 3 3 4 4 4 5. I'm still a bit unsure on why my loop isn't terminating when a becomes 5.

            – LowerMoon
            Mar 7 at 8:36














            Never mind, I understood. My loop just appends the element even if the element matches the element in the second list. I didn't add code to delete the already appended number if it finds an inequality in the other n-1 lists.

            – LowerMoon
            Mar 7 at 8:47





            Never mind, I understood. My loop just appends the element even if the element matches the element in the second list. I didn't add code to delete the already appended number if it finds an inequality in the other n-1 lists.

            – LowerMoon
            Mar 7 at 8:47













            2














            Similar implementation, but using an array to store the unique elements of the slice



            set lists [list $w $x $y $z]
            set common [list]

            for set i 0 $i < [llength $w] incr i
            array unset elements
            foreach list $lists
            set elements([lindex $list $i]) dummyvalue

            set unique [array names elements]
            if [llength $unique] == 1
            lappend common $unique



            puts $common ;# => 1 2 3 4





            share|improve this answer



























              2














              Similar implementation, but using an array to store the unique elements of the slice



              set lists [list $w $x $y $z]
              set common [list]

              for set i 0 $i < [llength $w] incr i
              array unset elements
              foreach list $lists
              set elements([lindex $list $i]) dummyvalue

              set unique [array names elements]
              if [llength $unique] == 1
              lappend common $unique



              puts $common ;# => 1 2 3 4





              share|improve this answer

























                2












                2








                2







                Similar implementation, but using an array to store the unique elements of the slice



                set lists [list $w $x $y $z]
                set common [list]

                for set i 0 $i < [llength $w] incr i
                array unset elements
                foreach list $lists
                set elements([lindex $list $i]) dummyvalue

                set unique [array names elements]
                if [llength $unique] == 1
                lappend common $unique



                puts $common ;# => 1 2 3 4





                share|improve this answer













                Similar implementation, but using an array to store the unique elements of the slice



                set lists [list $w $x $y $z]
                set common [list]

                for set i 0 $i < [llength $w] incr i
                array unset elements
                foreach list $lists
                set elements([lindex $list $i]) dummyvalue

                set unique [array names elements]
                if [llength $unique] == 1
                lappend common $unique



                puts $common ;# => 1 2 3 4






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 7 at 14:04









                glenn jackmanglenn jackman

                170k26147241




                170k26147241



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55038457%2fcompare-n-number-of-lists-in-tcl%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    1928 у кіно

                    Захаров Федір Захарович

                    Ель Греко