Reputation: 53
am beginner in Tcl/tk, facing problem in accessing array in procedure
below is my problem statement
proc myproc {args} {
set aaa ""
set bbb ""
set ccc ""
foreach "field value" $args {
set $field $value
}
# assigning input args values to an array a
set a(3:0) $aaa
set a(6:4) $bbb
set a(25:7) $ccc
#do some computation on input arguments may be addition
#
#
#
# now the result am trying to fetch into another array b
set $b(word0) $x
set $b(word1) $y
set $b(word2) $z
set $b(word3) $u
return [array get b]
}
now i need to pass arguments to myproc and return array i need to access.
set args_1 "g 1 h 4 k 6"
i tried the below syntax it was throwing me error.
array set a [myproc[array get $args_1]]
can somebody help me in solving this issue
trying to give string as input for procedure myproc
and later trying to do some computation with that input values.
later after all computation got set of string values, which are assigned to array as below
set $b(word0) $x
set $b(word1) $y
set $b(word2) $z
set $b(word3) $u
want to send this array b
as return.
example:
proc myproc {} {
set $b(word0) $x
set $b(word1) $y
set $b(word2) $z
set $b(word3) $u
return [array get b]
}
i have tried to access array b as below
array set a [myproc[array get b]]
it worked :) was able to creat new array in calling function.
but, need to pass string arguments to myproc and get return as array
Upvotes: 0
Views: 1592
Reputation: 114024
That function looks OK to me. There may be better ways to write it but it's essentially OK as is.
There are however a couple of problems with how you call that function.
First, you're confusing arrays and lists. In tcl, an array is a collection of key-value pairs. Other languages call this a "hash" or "map". A list is what is sounds like: a list of values. Other languages call this "array" or "list".
So, first off:
tcl other languages
--- ---------------
array = hash
list = array
The name "array" was chosen because the concept of a collection of key-value pairs is known in computer science as "associative arrays". Which is a term that predates the use of the word "array" to mean a list of values in languages like C and Java.
So, here you're declaring a list:
set args_1 "g 1 h 4 k 6"
And you're trying to access it as an array:
array get $args_1
Which should throw out an error that says that $args_1
is not an array. Which is in fact true.
So, simply replace it with the list variable:
$args_1
Which gives us:
array set a [myproc$args_1]
This should throw another error which basically says that the function myproc g 1 h 4 k 6
doesn't exist. Yes, in tcl it is valid to have whitespace in a function's name. For example:
proc "an example" {} {return "an example"}
That's valid code. And you call it like this:
set x ["an example"]
So it's no surprise that tcl can't find a function called "myproc g 1 h 4 k 6".
What this means is that whitespace is significant in tcl. You can't do:
set x [y[z]]
That's most likely a syntax error. It should be:
set x [y [z]]
# ^
# |______ whitespace was missing
So your code should be:
array set a [myproc $args_1]
Upvotes: 1