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.

list and join problems

Help for those learning Tcl or writing their own scripts.
Post Reply
M
Mukke
Voice
Posts: 6
Joined: Mon Oct 31, 2005 11:36 am

list and join problems

Post by Mukke »

i was searching for a replace command but i could only find lreplace somehow, ahwell i tried to work around then with lreplace wich agve me this script:

Code: Select all

proc iwa:topic {nick uhost handle chan arg} {
		global iwas db mq
		
		set flink ""
		set iwas(topic) [lrange $arg 0 100]
		set sitelink "\0030,3| http://www.iwaddicts.net/ |\003"
		set newlist [list $iwas(topic)] 
		# check if we have db conection
		if {[iwa:constructor $nick $uhost $handle $chan]} {
			
        	set qry "SELECT * 
					FROM  phpbb3_posts AS p
					LEFT JOIN phpbb3_users AS u ON p.poster_id=u.user_id
					ORDER BY  post_time DESC 
					LIMIT 1"
			set row [lindex [mysqlsel $db(sqlhand) $qry -list] 0]
			set toad [lindex $row 6]
			set tijd [ctime $toad]
			set user [lindex $row 33]
			set flink "| Latest discussion http://board.iwaddicts.net/[lindex $row 2],[lindex $row 1]#p[lindex $row 0] |"
			
		}
		#if requested the latest topic
		if([string first "--lt" [string tolower $arg]] != -1){
			set npl [lsearch $newlist "--lt"]
			set newlist [[lreplace $newlist $npl $npls $flink]]
		}
		#if requested the site url
		if([string first "--url" [string tolower $arg]] != -1)			
			set npl [lsearch $newlist "--url"]
			set newlist [[lreplace $newlist $npl $npls $sitelink]]
		}
		#make the topic and set it in the channel
		set newtopic [join $newlist]
		putserv "TOPIC $chan :$newtopic"
		putlog "#$handle# changed the topic in $chan to $newtopic"
}
error:
12:04] can't read "newlist": no such variable
while executing
"join $newlist"
invoked from within
"set newtopic [join $newlist]"
(file "scripts/iwa.tcl" line 178)
invoked from within
"source scripts/iwa.tcl"
M
Mukke
Voice
Posts: 6
Joined: Mon Oct 31, 2005 11:36 am

Post by Mukke »

when i tried to debug (commented out stuff) i get this:

Code: Select all

proc iwa:topic {nick uhost handle chan arg} {
		global iwas db mq
		
		set flink ""
		set iwas(topic) [lrange $arg 0 100]
		set sitelink "\0030,3 http://www.iwaddicts.net/\003"
		set newlist [lrange $arg 0 100]
		# check if we have db conection
		if {[iwa:constructor $nick $uhost $handle $chan]} {
			
        	set qry "SELECT * 
					FROM  phpbb3_posts AS p
					LEFT JOIN phpbb3_users AS u ON p.poster_id=u.user_id
					ORDER BY  post_time DESC 
					LIMIT 1"
			set row [lindex [mysqlsel $db(sqlhand) $qry -list] 0]
			set toad [lindex $row 6]
			set tijd [ctime $toad]
			set user [lindex $row 33]
			set flink "Latest discussion http://board.iwaddicts.net/[lindex $row 2],[lindex $row 1]#p[lindex $row 0]"
			
		}
		##if requested the latest topic
		#if([string first "--lt" [string tolower $arg]] != -1){
		#	set npl [lsearch $newlist "--lt"]
		#	set newlist [lreplace $newlist $npl $npls $flink]
		#}
		##if requested the site url
		#if([string first "--url" [string tolower $arg]] != -1)			
		#	set npl [lsearch $newlist "--url"]
		#	set newlist [lreplace $newlist $npl $npls $sitelink]
		#}
		#make the topic and set it in the channel
		#set newtopic [join $newlist]
		putserv "TOPIC $chan :$newlist"
		putlog "#$handle# changed the topic in $chan to $newtopic"
}
Error:
[12:18] can't read "chan": no such variable
while executing
"putserv "TOPIC $chan :$newlist""
(file "scripts/iwa.tcl" line 179)
invoked from within
"source scripts/iwa.tcl"
[/code]
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

That's not the full code and it looks like you not using lrange on a valid list.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Looking at the script I see multiple issues, including incompatible types, non-tcl syntax (half of the script looks like php), and other issues...
  • Code: Select all

    set iwas(topic) [lrange $arg 0 100]
    $arg is a string, not a list. If you are trying to grab the 100 first characters in the string, use string range.
  • Code: Select all

    set newlist [list $iwas(topic)]
    This would create a list with one single list-item, I'm quite sure this is not what you intended. If you wish to convert a string into a list, use the split command with suitable splitcharacters (works in a similar fashion to explode() in php).
  • Code: Select all

     if ([string first "--lt" [string tolower $arg]] != -1) {
    This would be php-syntax, not tcl-syntax. Very simplified, if expects the "test" to be a string containing one or more tcl-expressions. This string will then be passed to the tcl-interpreter and the result-code is then tested. As a rule of thumb, you (almost) always want to enclose the string with {} (to prevent early evaluation).
  • Code: Select all

    set newlist [[lreplace $newlist $npl $npls $flink]]
    This is very dangerous code, as it will try to execute the result from the lreplace-operation as a separate command, and set newlist to the result of this. In the tcl-preprocessor, [string] means execute string in the current interpreter, and replace [string] with whatever it returns, before the whole commandline is then passed to the current interpreter.
  • [12:18] can't read "chan": no such variable
    while executing
    "putserv "TOPIC $chan :$newlist""
    (file "scripts/iwa.tcl" line 179)
    invoked from within
    "source scripts/iwa.tcl"
    This error would suggest that the line putserv "TOPIC $chan :$newlist" was executed as the script was loaded, and not as part of the proc. That is, it would seem there are some unbalanced {} within your script, one of those being this part:

    Code: Select all

          #if requested the site url
          if([string first "--url" [string tolower $arg]] != -1)         
             set npl [lsearch $newlist "--url"]
             set newlist [[lreplace $newlist $npl $npls $sitelink]]
          }
    
    Here, there is no leading { to start the conditional code block, yet there is one } to terminate it -- which in this case terminates the whole proc instead.
I think this covers most of the issues in the posted code...
NML_375
Post Reply