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

passing arrays

Status
Not open for further replies.

zskater

Programmer
Apr 14, 2001
22
GB
if i have an array like

array set Cellinfo {
type 1
width 80
height 120
cost 50
color #D60000
}

and i want to call a procedure and pass it
the array name so the procedure can perform calculations
on the data in the array eg. startCell Cellinfo

proc startCell { } {
upvar


}


i cant work out how to use upvar and access the array from
within the procedure

Can someone please put me out of my 3 hour misery !

Cheers Steve
 
Here's how I do it:
proc dotP {a b} {
upvar $a x
upvar $b y
set dp 0
for {set i 1} {$i <= 3} {incr i} {
incr dp [expr {$x($i)*$y($i)}]
}
return $dp
}
In this example, a and b are the NAMES of arrays (in this case 1D arrays) in the global space. &quot;upvar&quot; makes those global arrays equivalent to the arrays x and y in the procedure space. Bob Rashkin
rrashkin@csc.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top