It isn't exactly clear where the nicks are coming from. However, the following should help. Your original format of nicks appears to be seperated by newline, as could be the case in a text file.
Code: Select all
# read a text file and create an array from the contents
# each line of the text file consists of a unique nick followed by data associated with that nick
proc readFile {} {
# the array will be given global scope so that it can be used elsewhere
global dataArray
# open datafile for reading
# create a list by splitting the file's contents at each newline character
# ignore any newline character after the last line
set id [open db.txt r]
set dataList [split [read -nonewline $id] \n]
close $id
# if dataList created above is not empty, populate dataArray
# first word of each dataList element is a nick and will become the array name
# remainder of each dataList element will become the array value
if {[llength $dataList] != 0} {
foreach line $dataList {
set dataArray([lindex $line 0]) [join [lrange $line 1 end]]
}
}
return 0
}
# create a string variable of comma-space delimited nicks from the element names of an array
proc listNicks {} {
global dataArray
# if dataArray is not empty, create a nickList from the names of it's elements
# convert the nickList to a nickString by joining with comma-space delimiter
# return the nickString to the calling proc
# zero (0) will be returned to the calling proc if dataArray is empty
if {[array size dataArray] != 0} {
set nickList [array names dataArray]
set nickString [join $nickList ", "]
return $nickString
}
return 0
}
To generalise, if you have a list and want to create a string, the core Tcl 'join' command is used.
join <list> ?joinString?
Without a joinString specified, a space character is used.
It is very important that you understand (in the context of Tcl) the format of what you are starting with and the format of what you wish to end up with. For example, the code pasted above might return :-
arfer, hernick, hisnick, anothernick
It looks like a list and might be referred to in plain English as a list. In Tcl it could not be treated as a list of nicks. It is a comma-space delimited string of nicks. My advise would be to keep the unique nicks in a list so that you always know what you are dealing with programmatically and only convert to a string if and when you want to output :-
Code: Select all
putserv "PRIVMSG #channelname :[join $nickList ", "]