user707549
user707549

Reputation:

how these array works in Tcl?

I have a question about array in tcl, how the following code works:

set Employee_Info_Array(EmployeeId,EmplogeePhoneNumber) "0 12345678 0 0 0"


proc setEmployee_Info_Array {{empId} {phoneNumber} {infoList}} {
    set  Employee_Info_Array($empId,$phoneNumber) $infoList
}

proc getEmployee_Info_Array {{empId} {phoneNumber}} {
    return Employee_Info_Array($empId,$phoneNumber);
}

the variable Employee_Info_Array is a array, and the initial value is "0 12345678 0 0 0", but when we call the setEmployee_Info_Array {1,87654321, "1 1 1"}, how this array is set? are there two entries in this array? like entry 1:"0 12345678 0 0 0" entry 2: "1,87654321,1 1 1"? when we use getEmployee_Info_Array(1 1), what we get?

Upvotes: 1

Views: 183

Answers (1)

RHSeeger
RHSeeger

Reputation: 16262

There's a whole lot wrong with the code you have here

set Employee_Info_Array(EmployeeId,EmplogeePhoneNumber) "0 12345678 0 0 0"

You now have an array with one key/value pair:

> parray Employee_Info_Array
Employee_Info_Array(EmployeeId,EmplogeePhoneNumber) = 0 12345678 0 0 0

I'm guessing what you really wanted was something like this:

set Employee_Info_Array(0,12345678) "0 0 0"
> parray Employee_Info_Array
Employee_Info_Array(0,12345678) = 0 0 0

The first command needs a global in order to actually use the same array:

proc setEmployee_Info_Array {{empId} {phoneNumber} {infoList}} {
   global Employee_Info_Array 
   set  Employee_Info_Array($empId,$phoneNumber) $infoList
}

The second command also needs global, plus it needs a $ in order to actually return the value in question. As it was, it returns the the string Employee_Info_Array($empId,$phoneNumber) with empId and phoneNumber substituted:

proc getEmployee_Info_Array {{empId} {phoneNumber}} {
    return Employee_Info_Array($empId,$phoneNumber);
}
getEmployee_Info_Array a b ;# returns literal value "Employee_Info_Array(a,b)"

This would be more correct:

proc getEmployee_Info_Array {{empId} {phoneNumber}} {
    global Employee_Info_Array ;# same deal, needs global
    return $Employee_Info_Array($empId,$phoneNumber);
}

Lastly, the way you list calling it will fail because you're passing in a single value

setEmployee_Info_Array {1,87654321, "1 1 1"}
wrong # args: should be "setEmployee_Info_Array empId phoneNumber infoList"
    while executing
"setEmployee_Info_Array {1,87654321, "1 1 1"}"

If you actually pass in the values the way you meant to, it works better. Remember, tcl commands are all of the form command ?arg1? ... ?argn?. You don't surround the list of arguments with braces, or separate each one with commas:

setEmployee_Info_Array  1 87654321  "1 1 1"

Putting it all together:

set Employee_Info_Array(0,12345678) "0 0 0"
proc setEmployee_Info_Array {{empId} {phoneNumber} {infoList}} {
    global Employee_Info_Array
    set  Employee_Info_Array($empId,$phoneNumber) $infoList
}
proc getEmployee_Info_Array {{empId} {phoneNumber}} {
    global Employee_Info_Array
    return Employee_Info_Array($empId,$phoneNumber);
}
puts "Initial value"
parray Employee_Info_Array
setEmployee_Info_Array 1 87654321  "1 1 1"
puts "After set"
parray Employee_Info_Array

Which gives as output:

Initial value
Employee_Info_Array(0,12345678) = 0 0 0
After set
Employee_Info_Array(0,12345678) = 0 0 0
Employee_Info_Array(1,87654321) = 1 1 1

Upvotes: 5

Related Questions