ThisUser_New
ThisUser_New

Reputation: 15

tcl loop through multiple lists

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.

Upvotes: 0

Views: 5461

Answers (2)

Peter Lewerin
Peter Lewerin

Reputation: 13252

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

Upvotes: 0

Dinesh
Dinesh

Reputation: 16428

% 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

Upvotes: 1

Related Questions