0

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.

2 Answers 2

1
% 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
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

If you've got something relevant and new to add, answering once a questioner has accepted an answer is reasonable. I've got a lot of rep from doing that where the questioner accepted an answer that I thought needed a different slant. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.