0

I do the following :

set q [list Phi1 Phi2 Phi3 Phi4 Phi5 Phi6 Phi7 Phi8 Phi9 Phi10 Phi11 Phi12 Phi13 Phi14 Phi15 Phi16 Phi17 Phi18 Phi19 Phi20 Phi21 Phi22 Phi23 Phi24 Phi25 Phi26 Phi27]

then I define my lists (from Phi1 to Phi27)

foreach l $q {
    for {set i 5} {$i<17} {incr i 1} {
        set fx1 [nodeEigenvector $i 1 1]
        set fy1 [nodeEigenvector $i 1 2]
        set frot1 [nodeEigenvector $i 1 3]
        lappend $l [list $fx1]
        lappend $l [list $fy1]
        lappend $l [list $frot1]
    }
}

Then I want to save those vector into a single file :

foreach aer $q {
    for {set re 1} {$re<27} {incr re 1} {
        set Mode $aer
        set fo [open Modd.out a]    
        puts $fo [list get $Mode]
        puts [list get $aer]
        close $fo
    }
}

This doesn't work. I get a file with a list of "get Phi1" (27 times...) to Phi27... funny fact, when I type the command puts [list get $Phi1] I do obtain my data as expected at screen. Anyone could help me? If there is a simpler way to do it I'd like to know too ! (I am simply trying to build, populate, then save a matrix (27 vector)).

1 Answer 1

1

There is no list get command. You can get the value of the list by doing a [set $aer].

[set varname] returns the value of varname, so if aer is set to Phi1, [set $aer] will return the value of Phi1.

Opening and closing your file every time is very inefficient, so I fixed that also.

So the last loop becomes:

set fo [open Modd.out a] ; # or mode w    
foreach aer $q {
   puts $fo [set $aer]
   puts [set $aer]
}
close $fo

You can rewrite this to use the dict command.

set phi [dict create]
foreach l $q {
    for {set i 5} {$i<17} {incr i 1} {
        set fx1 [nodeEigenvector $i 1 1]
        set fy1 [nodeEigenvector $i 1 2]
        set frot1 [nodeEigenvector $i 1 3]
        dict lappend phi $l [list $fx1 $fy1 $frot1]
        # or another way:
        # dict lappend phi $l $fx1 $fy1 $frot1
    }
}

puts $phi
puts [dict get $phi Phi2]

The dictionary can be structured in different ways depending on how you need to access your matrix.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, if you are using variable names in variables (except when feeding into upvar), consider whether an array would be a better solution. Though a dictionary as you show works too.

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.