Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read variable value present in one .tcl file from another .tcl

Status
Not open for further replies.

flowerbs

Programmer
May 11, 2010
4
IN
Hi,

For example if I have file1.tcl and file2.tcl

file1.tcl
----------
global flag
set flag 0


file2.tcl:
----------
Here i need to use the value of flag that is present in file1.tcl.


My obejctive is to read the value of a variable present in file1.tcl from file2.tcl.

Can anyone help me with small code snippet to achieve the same.

Thanks in advance.
 
Hi

Like this ?
Code:
[b]global[/b] flag
[b]set[/b] flag [purple]0[/purple]
Code:
[b]set[/b] flag [purple]2010[/purple]

[b]puts[/b] [green][i]"Before : $flag"[/i][/green]

[b]source[/b] file1[teal].[/teal]tcl

[b]puts[/b] [green][i]"After : $flag"[/i][/green]
Code:
[blue]master #[/blue]tclsh file2.tcl 
Before : 2010
After : 0

Feherke.
 
Feherke illustrated what I understood as well. But maybe you mean this?

file1.tcl:
set flag 1234
set output [exec tclsh /path/to/file2.tcl $flag]
puts stdout $output

file2.tcl:
set argc [llength $argv]
if {$argc < 1} {
puts "Too few arguments"
exit
}
# Assuming flag is the first argument to this script
set flag [lindex $argv 0]
puts "flag = $flag"

# test code to read argv
for {set i 0} {$i<$argc} {incr i} {
puts "arg $i: [lindex $argv $i]"
}

In this case, file2.tcl is dependent on file1.tcl -or- user passing an argument to file2.tcl upon invocation.

For example:

tclsh file2.tcl 1234

 
Hi,

Thank you very much for the quick response.

Actually I am working on an application wherein I need to read and based on the flag value obtained from one file, have to Pause the execution in the second file.

1st file contains many other operation like start execution, passing several commands etc.,

I can not source the 1st file as it starts executing the complete code present in 1st file.

Is there a way to just read the value of flag present in 1st file from second without having file1 sourced?

Thanks!
 
Hi

If part of file1 is parsed only once and other part is parsed multiple times then why are you keeping those pieces in the same file ? Sounds wrong and inefficient. Anyway.
Code:
[b]set[/b] some [purple]1[/purple]
[b]set[/b] flag [purple]0[/purple]
[b]set[/b] thing [purple]2[/purple]
Code:
[b]proc[/b] partialsource [teal]{[/teal]what where[teal]}[/teal] [teal]\[/teal]
[teal]{[/teal]
  [b]set[/b] FIL [teal][[/teal][b]open[/b] [navy]$where[/navy][teal]][/teal]
  [b]while[/b] [teal]{[/teal][teal]![[/teal][b]eof[/b] [navy]$FIL[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
    [b]set[/b] line [teal][[/teal][b]gets[/b] [navy]$FIL[/navy][teal]][/teal]
    [b]if[/b] [teal]{[/teal][teal][[/teal][b]lrange[/b] [navy]$line[/navy] [purple]0[/purple] [purple]1[/purple][teal]]==[/teal][green][i]"set $what"[/i][/green][teal]}[/teal] [teal]{[/teal]
      [b]global[/b] [navy]$what[/navy]
      [b]eval[/b] [navy]$line[/navy]
    [teal]}[/teal]
  [teal]}[/teal]
  [b]close[/b] [navy]$FIL[/navy]
[teal]}[/teal]

[b]set[/b] some [purple]2009[/purple]
[b]set[/b] flag [purple]2010[/purple]
[b]set[/b] thing [purple]2011[/purple]

[b]puts[/b] [green][i]"Before : $some $flag $thing"[/i][/green]

partialsource flag file1[teal].[/teal]tcl

[b]puts[/b] [green][i]"After : $some $flag $thing"[/i][/green]
Code:
[blue]master #[/blue] tclsh file2.tcl 
Before : 2009 2010 2011
After : 2009 0 2011

Feherke.
 
Hi

While I mentioned efficiency, if you have flag set in a single place then you can [tt]break[/tt] the loop when the first occurrence is found :
Code:
[b]proc[/b] partialsource [teal]{[/teal]what where[teal]}[/teal]  [teal]\[/teal]
[teal]{[/teal]
  [b]set[/b] FIL [teal][[/teal][b]open[/b] [navy]$where[/navy][teal]][/teal]
  [b]while[/b] [teal]{[/teal][teal]![[/teal][b]eof[/b] [navy]$FIL[/navy][teal]][/teal][teal]}[/teal] [teal]{[/teal]
    [b]set[/b] line [teal][[/teal][b]gets[/b] [navy]$FIL[/navy][teal]][/teal]
    [b]if[/b] [teal]{[/teal][teal][[/teal][b]lrange[/b] [navy]$line[/navy] [purple]0[/purple] [purple]1[/purple][teal]]==[/teal][green][i]"set $what"[/i][/green][teal]}[/teal] [teal]{[/teal]
      [b]global[/b] [navy]$what[/navy]
      [b]eval[/b] [navy]$line[/navy]
      [highlight][b]break[/b][/highlight]
    [teal]}[/teal]
  [teal]}[/teal]
  [b]close[/b] [navy]$FIL[/navy]
[teal]}[/teal]

Feherke.
 
What in the world are you trying to do? If variable flag is constant then why not set it in file2? If flag is being manipulated and modified then file1 must execute!!!

It seems you may need to modularize file1. By that I mean, you need to create procedures that do very specific tasks in file1.
1. Issue source to read the file1 from file2.
2. Then call on specific procedure in file1 from file2 to get your flag. Code example might help.

file1.tcl: (modularized)

# global data
set flag 1
puts "This is global scope"
puts "Any code here will be executed via source command"

set flag 2

# Only p1 is maniuplating flag value
proc p1 {a1 a2} {
global flag
set flag [expr $flag + $a1 + $a2]
return $flag
}

proc p2 {} {
puts "This is p2"
}

proc p3 {msg} {
puts "msg: $msg"
}

puts "Global scope still!"
puts "source command from file2 will execute this code too!"

file2.tcl:

set argc [llength $argv]
if {$argc < 1} {
puts "Too few arguments"
exit
}
# Assuming flag is the first argument to this script
set flag [lindex $argv 0]
puts "flag = $flag"

# test code to read argv
for {set i 0} {$i<$argc} {incr i} {
puts "arg $i: [lindex $argv $i]"
}

# This is now using source command!
# Note: Only global scope is visible!
source file1.tcl

# calls ONLY procedure p1 w/ 2 arguments in file1!
p1 10 20

# calls ONLY procedure p2 w/ no arguments in file1!
p2

If file1.tcl is lots of work to modularize then you could use the code as I illustrated in previous post with conditions around the code, i.e,

file1.tcl:
if {$flag == 2} {
set output [exec tclsh /path/to/file2.tcl $flag]
puts stdout $output
# no need to continue execution of file1!
exit 0
} else {
# file1 execution continues

}

Hope this helps.

Feherke, how do you put your code into a window? :p
Thanks

 
Hi

ExpectTheUnexpected said:
Feherke, how do you put your code into a window? :p
I put [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags around it.

Note that the Process TGML checkbox in the Reply form must be checked.

Note that syntax highlighting is done off-line using GNU [tt]source-highlight[/tt].

Feherke.
 
You can check how procedure p1 modified flag in file1.

Updated file2.tcl:

Code:
  # calls ONLY procedure p1 w/ 2 arguments in file1!
  set flag [p1 10 20]
  puts "flag in file2: $flag"


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top