tcl loop through multiple lists - foreach

I have two lists I'd like manipulate.. ( I am a tcl newbie..). I'd like to associate these two lists and create a third list with some data added.
The data I have:
set aes {ae0 ae3 ae6 ae1v1 ae1v8}
set c {c1 c2 c3 k1 k2}
foreach A $aes {
foreach C $c {
puts ${A}_$C
}
}
The data I get as you'd expect is:
ae0_c1
ae0_c2
ae0_c3
ae0_k1
ae0_k2
..
..
What I want to do is
append some data in front of this.
AE-To-c = All ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 .. End.

% set aes {ae0 ae3 ae6 ae1v1 ae1v8}
ae0 ae3 ae6 ae1v1 ae1v8
% set c {c1 c2 c3 k1 k2}
c1 c2 c3 k1 k2
% foreach A $aes {
foreach C $c {
# saving into 'result' variable
lappend result ${A}_${C}
}
}
% set data "some more here"
some more here
% set result
ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 ae3_c1 ae3_c2 ae3_c3 ae3_k1 ae3_k2 ae6_c1 ae6_c2 ae6_c3 ae6_k1 ae6_k2 ae1v1_c1 ae1v1_c2 ae1v1_c3 ae1v1_k1 ae1v1_k2 ae1v8_c1 ae1v8_c2 ae1v8_c3 ae1v8_k1 ae1v8_k2
% set result [linsert $result 0 $data]
some more here ae0_c1 ae0_c2 ae0_c3 ae0_k1 ae0_k2 ae3_c1 ae3_c2 ae3_c3 ae3_k1 ae3_k2 ae6_c1 ae6_c2 ae6_c3 ae6_k1 ae6_k2 ae1v1_c1 ae1v1_c2 ae1v1_c3 ae1v1_k1 ae1v1_k2 ae1v8_c1 ae1v8_c2 ae1v8_c3 ae1v8_k1 ae1v8_k2

Your question isn't 100% clear. Is it something like this you want?
set res [list AE-To-c = All]
foreach A $aes {
foreach C $c {
lappend res ${A}_$C
}
}
lappend res End
If you want to do what I think you want to do, you need to capture the permutations of the two lists in a list instead of printing them out, and then wrap that list in a prefix and suffix.
The method above pre-loads the result list with the AE-To-c = All prefix, then picks up the permutations using lappend, and finally adds the End suffix as a last element in the list.
Another way:
set res [list]
foreach A $aes {
foreach C $c {
lappend res ${A}_$C
}
}
concat [list AE-To-c = All] $res End
In this variant the list of permutations is created first, and then the prefix list, the permutation list, and the suffix list (yes, End is a list) are concatenated into one flat list.
Documentation: concat, foreach, lappend, list, set

Related

Generating all combinations from a table in Lua

I'm trying to iterate through a table with a variable amount of elements and get all possible combinations, only using every element one time. I've landed on the solution below.
arr = {"a","b","c","d","e","f"}
function tablelen(table)
local count = 0
for _ in pairs(table) do
count = count + 1
end
return count
end
function spellsub(table,start,offset)
local str = table[start]
for i = start+offset, (tablelen(table)+1)-(start+offset) do
str = str..","..table[i+1]
end
return str
end
print(spellsub(arr,1,2)) -- Outputs: "a,d,e" correctly
print(spellsub(arr,2,2)) -- Outputs: "b" supposed to be "b,e,f"
I'm still missing some further functions, but I'm getting stuck with my current code. What is it that I'm missing? It prints correctly the first time but not the second?
A solution with a coroutine iterator called recursively:
local wrap, yield = coroutine.wrap, coroutine.yield
-- This function clones the array t and appends the item new to it.
local function append (t, new)
local clone = {}
for _, item in ipairs (t) do
clone [#clone + 1] = item
end
clone [#clone + 1] = new
return clone
end
--[[
Yields combinations of non-repeating items of tbl.
tbl is the source of items,
sub is a combination of items that all yielded combination ought to contain,
min it the minimum key of items that can be added to yielded combinations.
--]]
local function unique_combinations (tbl, sub, min)
sub = sub or {}
min = min or 1
return wrap (function ()
if #sub > 0 then
yield (sub) -- yield short combination.
end
if #sub < #tbl then
for i = min, #tbl do -- iterate over longer combinations.
for combo in unique_combinations (tbl, append (sub, tbl [i]), i + 1) do
yield (combo)
end
end
end
end)
end
for combo in unique_combinations {'a', 'b', 'c', 'd', 'e', 'f'} do
print (table.concat (combo, ', '))
end
For a tables with consecutive integer keys starting at 1 like yours you can simply use the length operator #. Your tablelen function is superfluous.
Using table as a local variable name shadows Lua's table library. I suggest you use tbl or some other name that does not prevent you from using table's methods.
The issue with your code can be solved by printing some values for debugging:
local arr = {"a","b","c","d","e","f"}
function spellsub(tbl,start,offset)
local str = tbl[start]
print("first str:", str)
print(string.format("loop from %d to %d", start+offset, #tbl+1-(start+offset)))
for i = start+offset, (#tbl+1)-(start+offset) do
print(string.format("tbl[%d]: %s", i+1, tbl[i+1]))
str = str..","..tbl[i+1]
end
return str
end
print(spellsub(arr,1,2)) -- Outputs: "a,d,e" correctly
print(spellsub(arr,2,2)) -- Outputs: "b" supposed to be "b,e,f"
prints:
first str: a
loop from 3 to 4
tbl[4]: d
tbl[5]: e
a,d,e
first str: b
loop from 4 to 3
b
As you see your second loop does not ran as the start value is already greater than the limit value. Hence you only print the first value b
I don't understand how your code is related to what you want to achieve so I'll leave it up to you to fix it.

TCL foreach to keep track of index

When using foreach in TCL to loop through a list it is desired to have a running number of the index of the current object. A way I have done this before is to maintain an extra counter variable.
set ct 0
foreach x $list {
puts "Items is $x index is $ct"
incr ct
}
One could use lsreach to retrieve the index but that's compute intensive and could be problematic with double occurrences.
Wondering if there is streamlined sleek-looking way of maintaining index information during a loop.
Pseudocode :
foreach x $list {
puts "Items is $x index is [foreach_index $x]"
}
Your feedback is appreciated.
UPDATE:
Run time tests with the provided answers:
Peter Lewerin : 86098.8 microseconds per iteration
Gert : 91057.4 microseconds per iteration
David B : 115860.0 microseconds per iteration
Loop through list with 100k random strings 80char long.
The loop proc is fastest, but hard to read.
While the control structure definitions in other languages are the law, in Tcl they're more like a set of guidelines.
proc foreachWithIndex {indexVar args} {
upvar 1 $indexVar var
set var 0
uplevel 1 [list foreach {*}[lrange $args 0 end-1] "[lindex $args end];incr $indexVar"]
}
foreachWithIndex x v {a b c} {puts "$x: $v"}
But I suggest using for instead. Radical language modifications are fun and occasionally useful, but if I had an Imperial credit for every such clever construct I ended up throwing away later I could build my own Death Star and still have money to put a grating over the exhaust port.
Your method of using incr with a counter works ok. This also works:
for { set i 0 } { $i < [llength $list] } { incr i } {
set x [lindex $list $i]
puts "Items is $x index is $i"
}
Another advantage of doing it this way is that you can modify the list while you are iterating. Let's say you want to remove all items with the value "bad" from a list.
set values { 1 2 3 bad 4 bad bad 5 }
for { set i 0 } { $i < [llength $values] } { incr i } {
if { [lindex $values $i] eq "bad" } {
set values [lreplace $values $i $i]
incr i -1
}
}
puts $values

How to return values from foreach loop in tcl

I have a list of all the files in the directory. I have stored them in a variable file_list. I want to get the tail name for each file. My approach is like this.
set file_list [list /a/b/a.txt /a/b/b.txt /a/b/c/file1.tcl /a/b/c/file2.tcl]
proc file_tail {filename} {
set x {}
set f_tail [file tail $filename]
lappend x $f_tail
return $x
}
foreach ft $file_list {
set f_tail [file_tail $ft]
}
but f_tail only contains last value stored i.e. "file2.tcl" Please guide me. I want a list of all tail values of file
I suggest either:
set f_tail {}
foreach ft $file_list {
lappend f_tail [file_tail $ft]
}
or (if you have a later version of Tcl):
set f_tail [lmap ft $file_list {file_tail $ft}]
Documentation:
foreach,
lappend,
lmap (for Tcl 8.5),
lmap
If you are making a list of all the tails, do this:
set f_tail {}
foreach ft $file_list {
lappend f_tail [file tail $ft]
}
If your helper function is going to do the lappend, you need to keep the variable holding the list outside the procedure:
proc file_tail {filename listVariable} {
upvar 1 $listVariable theList
set f_tail [file tail $filename]
lappend theList $f_tail
}
set tails {}
foreach ft $file_list {
file_tail $ft tails ; # <<< NAME, so not $tails as that would READ the variable
}
Note that we are passing in the name of the variable (tails outside) and using upvar 1 inside the procedure to make a linked local variable (theList inside) that can be updated. However, you can't do it by passing in a list value; Tcl uses copy-on-write semantics for its values. You need to be careful about the difference between the names of variables and the values they contain; they're not the same.

How to compare two column in a spreadsheet

I have 30 columns and 1000 rows, I would like to compare column1 with another column. IF the value dont match then I would like to colour it red. Below is a small dataset in my spreadsheet:
A B C D E F ...
1 name sName email
2
3
.
n
Because I have a large dataset and I want to storing my columns in a array, the first row is heading. This is what I have done, however when testing I get empty result, can someone correct me what I am doing wrong?
var index = [];
var sheet = SpreadsheetApp.getActiveSheet();
function col(){
var data = sheet.getDataRange().getValues();
for (var i = 1; i <= data.length; i++) {
te = index[i] = data[1];
Logger.log(columnIndex[i])
if (data[3] != data[7]){
// column_id.setFontColor('red'); <--- I can set the background like this
}
}
}
From the code you can see I am scanning whole spreadsheet data[1] get the heading and in if loop (data[3] != data[7]) compare two columns. I do have to work on my colour variable but that can be done once I get the data that I need.
Try to check this tutorial if it can help you with your problem. This tutorial use a Google AppsScript to compare the two columns. If differences are found, the script should point these out. If no differences are found at all, the script should put out the text "[id]". Just customize this code for your own function.
Here is the code used to achieve this kind of comparison
function stringComparison(s1, s2) {
// lets test both variables are the same object type if not throw an error
if (Object.prototype.toString.call(s1) !== Object.prototype.toString.call(s2)){
throw("Both values need to be an array of cells or individual cells")
}
// if we are looking at two arrays of cells make sure the sizes match and only one column wide
if( Object.prototype.toString.call(s1) === '[object Array]' ) {
if (s1.length != s2.length || s1[0].length > 1 || s2[0].length > 1){
throw("Arrays of cells need to be same size and 1 column wide");
}
// since we are working with an array intialise the return
var out = [];
for (r in s1){ // loop over the rows and find differences using diff sub function
out.push([diff(s1[r][0], s2[r][0])]);
}
return out; // return response
} else { // we are working with two cells so return diff
return diff(s1, s2)
}
}
function diff (s1, s2){
var out = "[ ";
var notid = false;
// loop to match each character
for (var n = 0; n < s1.length; n++){
if (s1.charAt(n) == s2.charAt(n)){
out += "–";
} else {
out += s2.charAt(n);
notid = true;
}
out += " ";
}
out += " ]"
return (notid) ? out : "[ id. ]"; // if notid(entical) return output or [id.]
}
For more information, just check the tutorial link above and this SO question on how to compare two Spreadsheets.

Pandoc numbered example list doesn't work with custom writer

Pandoc has amazing extension example_lists for continuously numbered list throughout the document. We try to use custom writer to produce html, but numbering is broken in result html. Consider the following md-code:
(#) item 1
(#) item 2
## header ##
(#) item 3
Pandoc produces the following html-page by default:
1. item 1
2. item 2
header
3. item 3
But with custom writer (we took example with pandoc --print-default-data-file sample.lua) it produces:
1. item 1
2. item 2
header
1. item 3
Sample lua-writer contains the following code for ordered list processing:
function OrderedList(items)
local buffer = {}
for _, item in pairs(items) do
table.insert(buffer, "<li>" .. item .. "</li>")
end
return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
end
If add print for first elements of pairs from items table
function OrderedList(items)
local buffer = {}
for elem, item in pairs(items) do
print(elem)
table.insert(buffer, "<li>" .. item .. "</li>")
end
return "<ol>\n" .. table.concat(buffer, "\n") .. "\n</ol>"
end
we'll see just final list items' numbers:
1
2
1
So I don't think the problem is in the writer itself. Have you any ideas how to solve this problem?
Looking through pandoc sources for custom writer (src/Text/Pandoc/Writers/Custom.hs) you may find that OrderedList function actually gets four arguments, third of which is list style. You should be interested in Example list style. So you could update OrderedList implementation accordingly: introduce global variable for counting total items in Example-list, alter function code basing on list style (add start attribute in ol html tag for Example-list).
-- for counting examples (#)
local ExampleIdx = 1
function OrderedList(items, num, sty, delim)
local buffer = {}
for _, item in pairs(items) do
table.insert(buffer, "<li>" .. item .. "</li>")
end
local start = ""
if sty == "Example" then
if ExampleIdx > 1 then
start = ' start="' .. ExampleIdx .. '" '
end
ExampleIdx = ExampleIdx + table.getn(items)
end
return '<ol' .. start .. '>\n' .. table.concat(buffer, "\n") .. "\n</ol>"
end
You actually don't need to keep an ExampleIdx global variable as in Artem Pelenitsyn's answer. All you have to do is make your list item writer sensitive to the second parameter (the start number: num in Pelenitsyn's code). Note that you can use pandoc -t native to inspect the AST that is being passed to the writer; you'll see that the start number is set appropriately by the reader.
function OrderedList(items, num)
local buffer = {}
for _, item in pairs(items) do
table.insert(buffer, "<li>" .. item .. "</li>")
end
local start = ""
if num > 1 then
start = ' start="' .. num .. '" '
end
return '<ol' .. start .. '>\n' .. table.concat(buffer, "\n") .. "\n</ol>"
end

Resources