Compare 'n' number of lists in Tcl2019 Community Moderator ElectionTCL 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
Good allowance savings plan?
Is there a window switcher for GNOME that shows the actual window?
Grey hair or white hair
Latest web browser compatible with Windows 98
infinitive telling the purpose
What is the likely impact of grounding an entire aircraft series?
How do I locate a classical quotation?
What Happens when Passenger Refuses to Fly Boeing 737 Max?
Do I really need to have a scientific explanation for my premise?
Why would one plane in this picture not have gear down yet?
Is it true that real estate prices mainly go up?
Accountant/ lawyer will not return my call
Logic. Truth of a negation
How do you like my writing?
Should I tell my boss the work he did was worthless
Virginia employer terminated employee and wants signing bonus returned
What does a stand alone "T" index value do?
The bar has been raised
Am I not good enough for you?
My story is written in English, but is set in my home country. What language should I use for the dialogue?
Should QA ask requirements to developers?
Why the color red for the Republican Party
Replacing Windows 7 security updates with anti-virus?
Rejected in 4th interview round citing insufficient years of experience
Compare 'n' number of lists in Tcl
2019 Community Moderator ElectionTCL 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
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
add a comment |
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
add a comment |
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
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
tcl
asked Mar 7 at 7:40
LowerMoonLowerMoon
315
315
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
xagainst listx?
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]
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 inl1(or)was my reference and skip it during the comparison. Also, the reason I added&& [lindex $l2 $j] ==was because I was getting1 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 whenabecomes5.
– 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
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
xagainst listx?
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]
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 inl1(or)was my reference and skip it during the comparison. Also, the reason I added&& [lindex $l2 $j] ==was because I was getting1 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 whenabecomes5.
– 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
add a comment |
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
xagainst listx?
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]
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 inl1(or)was my reference and skip it during the comparison. Also, the reason I added&& [lindex $l2 $j] ==was because I was getting1 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 whenabecomes5.
– 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
add a comment |
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
xagainst listx?
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]
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
xagainst listx?
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]
answered Mar 7 at 8:02
JerryJerry
59.6k1070106
59.6k1070106
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 inl1(or)was my reference and skip it during the comparison. Also, the reason I added&& [lindex $l2 $j] ==was because I was getting1 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 whenabecomes5.
– 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
add a comment |
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 inl1(or)was my reference and skip it during the comparison. Also, the reason I added&& [lindex $l2 $j] ==was because I was getting1 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 whenabecomes5.
– 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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Mar 7 at 14:04
glenn jackmanglenn jackman
170k26147240
170k26147240
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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