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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to input a data array into a proc

Status
Not open for further replies.

mmoohhaammaadd

Programmer
Apr 8, 2017
3
IR
I want to input a vector "a" (or matrix) to a procedure and do some algebraic operations with its elements:

set a(1) 2;
set a(2) 3;

proc mult {a} {
global b
set b [expr $a(1)*$a(2)]
}
mult $a
puts "$b"

when I run this script it says:
can't read "a": variable is array

can anyone help me and write the correct procedure for this purpose?
 
can't read "a": variable is array

That's because it IS an array

set a(1) 2;
set a(2) 3;

Will create an associative array 'a' with two elements named '1' and '2' containing the values 2 & 3 respectively.


Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
Thank you so much for your reply. I knew that "a" is an array, but I don't know how to use it as an input to a procedure and use its elements in the proc.
Would you please write the simple procedure of importing array "a" into a proc named mult?
or just provide a simple script or example of it.

I find some tips here,
but I couldn't understand how to do it.
 
By the way;
mmoohhaammaadd said:
and write the correct procedure for this purpose?
Tek-Tips is NOT a code writing service, we will tell your where you may be going awry but we will not write the code for you.


mmoohhaammaadd said:
but I don't know how to use it as an input to a procedure and use its elements in the proc.
Okay, so at what point is the error being reported?

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.

Never mind this jesus character, stars had to die for me to live.
 
mmoohhaammaadd said:
knew that "a" is an array, but I don't know how to use it as an input to a procedure and use its elements in the proc.

for example like this
Code:
set a(1) 2
set a(2) 3

proc mult {[highlight #FCE94F]aa[/highlight]} {
  global b
  [highlight #FCE94F]upvar $aa a[/highlight]
  set b [expr $a(1)*$a(2)]
}
mult a
puts "$b"


 
Dear mikrom,
many thanks for your reply. it is solved by your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top