Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Simple Array problem(unable to pass to a procedure)

795 views
Skip to first unread message

anand kumar

unread,
May 31, 2012, 7:09:25 AM5/31/12
to
Hi

I am a newbie to TCL/TK. I wrote a simple program to create a data
structure using arrays. The following is the code


-----------------------------------------------------------------------------------------------------------------------------------------------------------

#Following is the procedure which creates a data structure using
arrays for an argument list passed to it

proc create_data_table {arg_list} {

puts "Argument list passed to procedure : $arg_list"

foreach {key data} $arg_list {
set data_table($key) $data
puts "Key $key : Data $data_table($key)"
}
}


#Following is the data processing procedure which takes a data
structure as an argument

proc data_process {data_table_list} {
array set my_data_table_list $data_table_list
foreach {key} $my_data_table_list {
puts "Processing $key with Data $my_data_table_list($key)"
}
puts "End of dummy process\n"
}


#Following is the declaration of my data_structure as a global

global data_table
array set data_table {}


#Following is the simple loop to create a list from command line
arguments

set arglist {}

foreach argument $argv {
lappend arglist $argument
}

puts $arglist

# Create the data structure by calling the process "create data table"
create_data_table $arglist

# Call the data processing procedure by serializing the array through
the list using "array get" command
data_process [array get $data_table]

-----------------------------------------------------------------------------------------------------------------------------------------------------------

I save the file as arr_data_struct.tcl and in my cygwin terminal, I
run the following
%tclsh arr_data_struct.tcl data1 key1 data2 key2 data3 key3 data4 key4

The error that flags is as follows :-

can't read "map_table": variable is array
while executing "data_process [array get $data_table]"
(file "dummy_map_test.tcl" line 41)


Please help me in understanding the error and what s going wrong

Thank you

Eugene

unread,
May 31, 2012, 9:04:54 AM5/31/12
to
On 31.05.2012 15:09, anand kumar wrote:

> data_process [array get $data_table]

I think you should really use [ array get data_table ]

--
Best regards, Eugene.

Arjen Markus

unread,
May 31, 2012, 9:11:30 AM5/31/12
to
Op donderdag 31 mei 2012 13:09:25 UTC+2 schreef anand kumar het volgende:
Arrays in Tcl are actually collections of variables. As such they
can not be passed "by value" as with all other variables. What you
can do instead is:

proc data_process {array_name} {
upvar 1 $array_name data_array

...
}

This way, you connect the array whose name you pass to the procedure
data_process to a local array with the name "data_array". You can
get the array elements and you can change the values or add new ones.

Another solution (especially useful if you only want to look at the
data that passed, not change them) is to use a list of data or a
dict. The latter is a kind of combination of a plain list and an array
in that you can select data via a key instead of an index.

Regards,

Arjen

Charlie Bursell

unread,
May 31, 2012, 9:12:52 AM5/31/12
to
You have to address an array by reference and not by value.

Change this: data_process [array get $data_table]

TO: data_process [array get data_table]
0 new messages