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.

lreplace to single value...?

Help for those learning Tcl or writing their own scripts.
Post Reply
S
StarLion
Voice
Posts: 16
Joined: Tue Dec 20, 2005 6:15 pm

lreplace to single value...?

Post by StarLion »

Code: Select all

    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?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
S
StarLion
Voice
Posts: 16
Joined: Tue Dec 20, 2005 6:15 pm

Post by StarLion »

i've tried

Code: Select all

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

Code: Select all

global cardcounts
...
S
StarLion
Voice
Posts: 16
Joined: Tue Dec 20, 2005 6:15 pm

Post by StarLion »

Well, let me review.
Here's the proc chain that's being called:

Code: Select all

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: Select all

    set newdeckc [lindex $deck $testcard]
    incr newdeckc 1
resulting in the error:

Code: Select all

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 :(
S
StarLion
Voice
Posts: 16
Joined: Tue Dec 20, 2005 6:15 pm

Post by StarLion »

okay, nevermind, i'm a doofus. Forgot the $ in front of the 'cardcounts' in the lreplace...
Post Reply