| View previous topic :: View next topic |
| Author |
Message |
StarLion Voice
Joined: 20 Dec 2005 Posts: 16
|
Posted: Thu Jun 28, 2007 10:42 am Post subject: lreplace to single value...? |
|
|
| Code: |
set newcardc [lindex $cardcounts $target]
incr newcardc 1
lreplace $cardcounts $target $target $newcardc
putlog "Replace Node: Attempt: $newcardc Actual: [lindex $cardcounts $target]" |
This code outputs "Replace Node: Attempt: 1 Actual: 0"... indicating my lreplace didnt actually DO anything... what am i doing wrong? |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Jun 28, 2007 10:49 am Post subject: |
|
|
lreplace expects the first variable to be a list, and will return a modified list.
Try saving the output of lreplace, and you'll find it working like a charm...
You'll find a few good examples at the manpage for lreplace aswell. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
StarLion Voice
Joined: 20 Dec 2005 Posts: 16
|
Posted: Thu Jun 28, 2007 12:56 pm Post subject: |
|
|
i've tried | Code: | | set cardcounts [lreplace $cardcounts $target $target $newcardc] |
but this makes $cardcounts a local variable, and destroys it (AND the global copy) at end of proc... which is very odd to me considering $cardcounts is locally created using ... |
|
| Back to top |
|
 |
StarLion Voice
Joined: 20 Dec 2005 Posts: 16
|
Posted: Thu Jun 28, 2007 1:29 pm Post subject: |
|
|
Well, let me review.
Here's the proc chain that's being called:
| Code: |
proc startgame {nick host hand chan text} {
global players gamestate hands coloring coloringend deckstring gamechan
if {$chan == $gamechan && $gamestate == 2 && [llength $players] > 1} {
set gamestate 3
putquick "PRIVMSG $chan : $nick has started the game! Dealing out the cards..."
repopdeck
for { set walker 0 } { $walker < [llength $players] } { incr walker } {
draw 7 $walker
set output "Your Cards:"
foreach card $hands($walker) {
append output " \003"
append output [lindex $coloring [lsearch $deckstring $card]] $card
append output "\003"
}
putquick "NOTICE [lindex $players $walker] : $output"
}
nextplayer
}
}
proc draw {number target} {
global cardcounts hands deck decktotal deckstring
for { set start 0 } { $start < $number } { incr start } {
set flag 0
if { $decktotal == 0 } { repopdeck }
while { $flag == 0 } {
set testcard [rand 50]
if { [lindex $deck $testcard] < 4 } { set flag 1 }
}
set newdeckc [lindex $deck $testcard]
incr newdeckc 1
set deck [lreplace $deck $testcard $testcard $newdeckc]
set newcardc [lindex $cardcounts $target]
putlog "Test Node: Target: $target Cardcountvalue: $newcardc"
incr newcardc 1
set cardcounts [lreplace cardcounts $target $target $newcardc]
putlog "Replace Node: Attempt: $newcardc Actual: [lindex $cardcounts $target]"
lappend hands($target) [lindex $deckstring $testcard]
}
incr decktotal -1
}
|
(Note: The putlogs are my attempts at debugging)
The initial values are:
$cardcounts {0 0}
$deck : A list of 50 zeros.
$hands: An array of lists, referenced numerically ($hands(0) {})
After the first walk through StartGame's loop, $cardcounts has become the static value 7 (no longer a list of two items). The script then promptly borks as the second loop goes into draw, and does this:
| Code: |
set newdeckc [lindex $deck $testcard]
incr newdeckc 1
|
resulting in the error:
| Code: |
Currently: expected integer but got ""
Currently: while executing
Currently: "incr newcardc 1"
Currently: (procedure "draw" line 15)
Currently: invoked from within
Currently: "draw 7 $walker"
Currently: (procedure "startgame" line 9)
Currently: invoked from within
Currently: "startgame $_pub1 $_pub2 $_pub3 $_pub4 $_pub5" |
Oddly, it works correctly on the deck..... but the cardcounts screw up  |
|
| Back to top |
|
 |
StarLion Voice
Joined: 20 Dec 2005 Posts: 16
|
Posted: Thu Jun 28, 2007 5:22 pm Post subject: |
|
|
| okay, nevermind, i'm a doofus. Forgot the $ in front of the 'cardcounts' in the lreplace... |
|
| Back to top |
|
 |
|