This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Passing Arrays to and from a procedure

Old posts that have not been replied to for several years.
Locked
j
juno

Passing Arrays to and from a procedure

Post by juno »

Hi,
I'm new to tcl and I'm having trouble passing arrays to and from procedures. I know it has something to do with upvar, but I can't find any documentation on it. Could someone please give an example?


Thanks,

Juno
t
tainted
Master
Posts: 239
Joined: Sun May 12, 2002 8:00 pm
Location: chicago
Contact:

Post by tainted »

I'm not too sure exactly what you are trying to do, but here is a link for documentation on 'upvar' and the rest of the tcl commands. tcl-commands.txt or whatever comes with eggdrop only lists the eggdrop specific commands and their uses. http://www.tcl.tk/man/
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Arrays can be passed to and from procedures with ease, and don't need any special treatment.

See netbots.tcl for a lot of helpful examples of this. Something like the following.

Code: Select all

set arr(item1) "value2"
set arr(item3) "value4"

proc test5 {input1 input2} {
  puts stdout "STANDARD VAR: $input1"
  foreach a [array names input2] {
    puts stdout "ARRAY: name = $a : value = $input2($a)"
  }
}

proc test6 {input1} {
  global arr
  puts stdout "STANDARD VAR: $input1"
  foreach a [array names arr] {
    puts stdout "ARRAY: name = $a : value = $arr($a)"
  }
}

test5 "hello" $arr
test6 "hello"
Both the test5 and test6 procedure output the same thing, the only differance being,t he way these arrays are passed.

test5 is genraly safer, as you can modify the values within the array, without them becoming global.
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

ppslim wrote:Arrays can be passed to and from procedures with ease, and don't need any special treatment.

See netbots.tcl for a lot of helpful examples of this. Something like the following.

Code: Select all

set arr(item1) "value2"
set arr(item3) "value4"

proc test5 {input1 input2} {
  puts stdout "STANDARD VAR: $input1"
  foreach a [array names input2] {
    puts stdout "ARRAY: name = $a : value = $input2($a)"
  }
}

proc test6 {input1} {
  global arr
  puts stdout "STANDARD VAR: $input1"
  foreach a [array names arr] {
    puts stdout "ARRAY: name = $a : value = $arr($a)"
  }
}

test5 "hello" $arr
test6 "hello"
Both the test5 and test6 procedure output the same thing, the only differance being,t he way these arrays are passed.

test5 is genraly safer, as you can modify the values within the array, without them becoming global.
Doing this (for test5) would only generate a 'can't read "arr": variable is array' error.
What I think you may have meant is the following:

Code: Select all

proc test5 {input1 input2} {
  global $input2 ;# ofcourse, the array needs to be set globally
  puts stdout "STANDARD VAR: $input1"
  foreach a [array names $input2] {
    puts stdout "ARRAY: name = $a : value = [lindex [array get $input2 $a] 1]" ;# split not needed since array get already returns a -list-
  }
}  
And to be called with a string representing the array name, such as:

Code: Select all

test5 "hello" "arr"
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

OOPS - I am have been seeling with too many PHP script lately.

There is that option, but the actual one I was aiming at, which will provide the protection I was talking about is.

Code: Select all

proc test5 {input1 input2} {
  puts stdout "STANDARD VAR: $input1"
  array set arr $input2
  foreach a [array names $arr] {
    puts stdout "ARRAY: name = $a : value = $arr($a)"
  }
}

test5 "hello" [array get arr]
This would allow the array to be passed over the proc arguments, as my first example had intended. It's entirly up to you how you do it.
j
juno

Post by juno »

Thanks for the examples guys!

I also come from a php background which is why I was having so much trouble understanding arrays in tcl.

BTW, I found a great explanation here.

Thanks again,

Juno
Locked