INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

how to input a data array into a proc

how to input a data array into a proc

(OP)
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?

RE: how to input a data array into a proc

Quote:

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.

https://www.tcl.tk/man/tcl8.5/TclCmd/set.htm

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.

RE: how to input a data array into a proc

(OP)
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,
http://stackoverflow.com/questions/3513266/passing...
but I couldn't understand how to do it.

RE: how to input a data array into a proc

By the way;

Quote (mmoohhaammaadd)

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.


Quote (mmoohhaammaadd)

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.

RE: how to input a data array into a proc

Quote (mmoohhaammaadd)


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 {aa} {
  global b
  upvar $aa a
  set b [expr $a(1)*$a(2)]
}
mult a
puts "$b" 


RE: how to input a data array into a proc

(OP)
Dear mikrom,
many thanks for your reply. it is solved by your help.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close