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 elements between braces how to search?

Help for those learning Tcl or writing their own scripts.
Post Reply
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

List elements between braces how to search?

Post by juanamores »

I've managed to save a list of nicks on line 0 in a text file.

The problem I have is when search and find nicks that list that begin and end with braces.

Example:
The nicknames variable is stored the full list of nicks.
$nicknames = pedro juan {FoSFaTiTo} maria susan
When I fetch the nick {FoSFaTiTo} on that list, the code takes the argument WITHOUT braces (the nick contains those braces), for that reason can not find it in the list.

Code: Select all

## !list [nickname]
bind pub n !list list_out_of_fd
proc list_out_of_fd {nick uhost hand chan arg} {
  global nchan
  if {[string tolower $chan] != [string tolower $nchan]} {
   return
  }
  if {[llength $arg] == 0} {
##here nicknames variables take values of list##
   set nicknames [getinfo]
   if {$nicknames == ""} {
     puthelp "NOTICE $nick :No one is added in the database!"
   } else {
     puthelp "NOTICE $nick :Added in the database: $nicknames"
   }
  } elseif {[llength $arg] == 1} {
##here nicknames variables take values of list##
   set nicknames [getinfo]
#For debuggin purposes#
   putmsg $nchan "The list: $nicknames"
   set arg [lindex $arg 0]
#For debuggin purposes#  
 putmsg $nchan "Nick to search: $arg"
   if {[lsearch -exact [string tolower $nicknames] [string tolower $arg]] == -1} {
     puthelp "NOTICE $nick :$arg not found in the database!"
   } else {
     puthelp "NOTICE $nick :$arg  is in the database!"
   }
  } else {
   puthelp "NOTICE $nick :Usage: !list \[nickname\]"
  }
}
Result:
<oper> !list {FoSFaTiTo}
<bot> The list: pedro juan {FoSFaTiTo} maria susan
<bot> Nick to search: FoSFaTiTo
<bot> FoSFaTiTo not found in the database!
Any idea how to solve this problem with nicks containing braces?

EDIT:
Another thing I've found if I show the contents of the variable that contains the list, the list looks good by the channel.
Example:

Code: Select all

putmsg #canal "$nicknames"
result:
pedro juan {FoSFaTiTo} maria susan
but if I do a foreach or use any other loop in the variable returns the nick me without the braces.

Code: Select all

foreach n $nicknames {
putmsg #canal "$n"
}
result:
pedro
juan
FoSFaTiTo
maria
susan
It's so hard to fix this in Tcl?
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

result:

Quote:
pedro juan {FoSFaTiTo} maria susan
That's not a list! It's a space separated string.
You need to split it into a proper tcl list at the spaces before treating it as a list...

Code: Select all

foreach n [split $nicknames] { 
  putmsg #canal "$n" 
}
SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
w
willyw
Revered One
Posts: 1205
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

...
set nicknames [getinfo]
...
It looks like you have some other scripts, or parts to this script, that create a proc named
getinfo

Please show us the whole script.

I'm curious to see how that
getinfo
makes the list, etc.
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

SpiKe^^ wrote:
result:

Quote:
pedro juan {FoSFaTiTo} maria susan
That's not a list! It's a space separated string.
You need to split it into a proper tcl list at the spaces before treating it as a list...

Code: Select all

foreach n [split $nicknames] { 
  putmsg #canal "$n" 
}
Thanks SpiKe^^, when I go home I try to change the code so advised.
willyw wrote: It looks like you have some other scripts, or parts to this script, that create a proc named
getinfo

Please show us the whole script.

I'm curious to see how that
getinfo
makes the list, etc.
The full code I have already posted in other threads.
See here.

And the proc getinfo, part of the total code is:

Code: Select all

proc getinfo {} {
  global filename
  set file [open $filename r]
  set nlist ""
  while {![eof $file]} {
   set chast [gets $file]
    if {$chast != ""} {
     append nlist $chast
   }
  }
  close $file
  return $nlist
}
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
w
willyw
Revered One
Posts: 1205
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

juanamores wrote: ...
And the proc getinfo, part of the total code is:

Code: Select all

proc getinfo {} {
  global filename
  set file [open $filename r]
  set nlist ""
  while {![eof $file]} {
   set chast [gets $file]
    if {$chast != ""} {
     append nlist $chast
   }
  }
  close $file
  return $nlist
}

I created a text file that looks like this:

Code: Select all

pedro
juan
{FoSFaTiTo}
maria
susan
with one nick on each line.
Each is on its own line.
Created with plain text editor - nano.

The proc quote above returns this:

.tcl getinfo
<rog> Tcl: pedrojuan{FoSFaTiTo}mariasusan

Are you sure that is the exact
getinfo
proc that you are using? Because... that's a mess. :)
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

willyw wrote:
I created a text file that looks like this:

Code: Select all

pedro
juan
{FoSFaTiTo}
maria
susan
with one nick on each line.
Each is on its own line.
Created with plain text editor - nano.

The proc quote above returns this:

.tcl getinfo
<rog> Tcl: pedrojuan{FoSFaTiTo}mariasusan

Are you sure that is the exact
getinfo
proc that you are using? Because... that's a mess. :)
The code does not work that way friend.
This code stores all nicks in line 1 of the text file, not one per line.
In addition, we have lacked copy another part of the total code where proc is used to remove the spaces.

Code: Select all

 set nicknames [removespaces $nicknames]

Code: Select all

proc removespaces {arg} {
  regsub {^\s+} $arg "" arg
  return $arg
}
SpiKe^^,there is a part that I could not solve.
See if you help me, the solution should be easy, but I could not fix the code.
Example:

Code: Select all

$nicknames = mario pedro luis {susan} {[polo]}

Code: Select all

proc del_in_fd {nick uhost hand chan text} {
##variable who added nick to delete##
set who [lindex $text 0]
##nicknames variable  stored values ​​I said above.##
nicknames [getinfo] 
##Compare who (nick to delete) with the value of the variable nicknames above.##
  if {[lsearch -exact [string tolower $nicknames] [string tolower $who]] == -1} {
   putmsg $canal_admin "$who not found in the database!"
   return 0
  } else {
##variable i added index nick to delete##
 set i [lsearch -exact [string tolower $nicknames] [string tolower $who]]
##Delete index containing the nick in the string.###
set nicknames [lreplace $nicknames $i $i]
###for debuggin purpose### 
putmsg $canal_admin "value nicknames: $nicknames"
#####HERE PROBLEM######
<oper> !del pedro
<bot> value nicknames: mario luis susan {[polo]}
Eliminate pedro, but it takes away the brace to susan.
Where I have to put the split when you delete a nick not remove braces susan? :oops:
Or
Is there another way to remove nicks the string without susan lose your braces?
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
w
willyw
Revered One
Posts: 1205
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

juanamores wrote: ...
The code does not work that way friend.
This code stores all nicks in line 1 of the text file, not one per line.
This is made much more difficult, because you are not giving us everything that you are working with.
In addition, we have lacked copy another part of the total code ...
... sigh...

where proc is used to remove the spaces.

Code: Select all

 set nicknames [removespaces $nicknames]

Code: Select all

proc removespaces {arg} {
  regsub {^\s+} $arg "" arg
  return $arg
}
And who knows why....

I believe that with a re-write of your method, not only would it be easier to get the correct result, it might even be easier to see it happen.

I think I will just bow out and wait. :)
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

willyw wrote: This is made much more difficult, because you are not giving us everything that you are working with.
Dear friend, of course I have given the full code on several occasions, TCL is the same we have been studying in other threads.. :D

I did not put full at this time, since the code is very complex, and do not want to confuse them when applying concrete help with a small part of it.

What I will share them, my doubts are small pieces of code that achievement not fix.
Anyway I have adapted the original TCL to my needs, so it is not identical.

I've shared part of getinfo proc, you were curious as to how it worked the same, but that part and have dominated the list to add or nicks.

If you want to use only the getinfo for the purpose of achieving the same results that I managed process, you have to create a text file and put the first line of the same, all nicks separated by a space.
eg:
LINE 1 of your txt file look like this:

Code: Select all

pedro juan {FoSFaTiTo} maria susan 
As SpiKe^^ said earlier, the result stored in a variable is a string separated by spaces.

Anyway when I get finished all the code I'll share with you completely. :D

I've solved the processes when added as the nicks are listed, so that they are stored in text format without losing any characters (eg containing braces).
That I succeeded, with the help of SpiKe^^ who advised me to use the split command.

The only part I have left unfixed is the procedure where a nick from the list is deleted, prompting my new consulting SpiKe^^.
juanamores wrote: SpiKe^^,there is a part that I could not solve.
See if you help me, the solution should be easy, but I could not fix the code.
Example:

Code: Select all

$nicknames = mario pedro luis {susan} {[polo]}	

Code: Select all

proc del_in_fd {nick uhost hand chan text} {
##variable who added nick to delete##
set who [lindex $text 0]
##nicknames variable  stored values ​​I said above.##
nicknames [getinfo]
##Compare who (nick to delete) with the value of the variable nicknames above.##
  if {[lsearch -exact [string tolower $nicknames] [string tolower $who]] == -1} {
   putmsg $canal_admin "$who not found in the database!"
   return 0
  } else {
##variable i added index nick to delete##
 set i [lsearch -exact [string tolower $nicknames] [string tolower $who]]
##Delete index containing the nick in the string.###
set nicknames [lreplace $nicknames $i $i]
###for debuggin purpose###
putmsg $canal_admin "value nicknames: $nicknames"
#####HERE PROBLEM######
<oper> !del pedro
<bot> value nicknames: mario luis susan {[polo]}

Eliminate pedro, but it takes away the brace to susan.
Where I have to put the split when you delete a nick not remove braces susan? Embarassed
Or
Is there another way to remove nicks the string without susan lose your braces?
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

juanamores wrote:
willyw wrote: This is made much more difficult, because you are not giving us everything that you are working with.
Dear friend, of course I have given the full code on several occasions, TCL is the same we have been studying in other threads.. :D

I did not put full at this time, since the code is very complex, and do not want to confuse them when applying concrete help with a small part of it.

What I will share them, my doubts are small pieces of code that achievement not fix.
Anyway I have adapted the original TCL to my needs, so it is not identical.

I've shared part of getinfo proc, you were curious as to how it worked the same, but that part and have dominated the list to add or nicks.

If you want to use only the getinfo for the purpose of achieving the same results that I managed process, you have to create a text file and put the first line of the same, all nicks separated by a space.
eg:
LINE 1 of your txt file look like this:

Code: Select all

pedro juan {FoSFaTiTo} maria susan 
As SpiKe^^ said earlier, the result stored in a variable is a string separated by spaces.

Anyway when I get finished all the code I'll share with you completely. :D

I've solved the processes when added as the nicks are listed, so that they are stored in text format without losing any characters (eg containing braces).
That I succeeded, with the help of SpiKe^^ who advised me to use the split command.

The only part I have left unfixed is the procedure where a nick from the list is deleted, prompting my new consulting SpiKe^^.
juanamores wrote: SpiKe^^,there is a part that I could not solve.
See if you help me, the solution should be easy, but I could not fix the code.
Example:

Code: Select all

$nicknames = mario pedro luis {susan} {[polo]}	

Code: Select all

proc del_in_fd {nick uhost hand chan text} {
##variable who added nick to delete##
set who [lindex $text 0]
##nicknames variable  stored values ​​I said above.##
nicknames [getinfo]
##Compare who (nick to delete) with the value of the variable nicknames above.##
  if {[lsearch -exact [string tolower $nicknames] [string tolower $who]] == -1} {
   putmsg $canal_admin "$who not found in the database!"
   return 0
  } else {
##variable i added index nick to delete##
 set i [lsearch -exact [string tolower $nicknames] [string tolower $who]]
##Delete index containing the nick in the string.###
set nicknames [lreplace $nicknames $i $i]
###for debuggin purpose###
putmsg $canal_admin "value nicknames: $nicknames"
#####HERE PROBLEM######
<oper> !del pedro
<bot> value nicknames: mario luis susan {[polo]}

Eliminate pedro, but it takes away the brace to susan.
Where I have to put the split when you delete a nick not remove braces susan? Embarassed
Or
Is there another way to remove nicks the string without susan lose your braces?
So we will wait the kind assistance of SpiKe^^ for the purpose of completing the code. :)
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
User avatar
SpiKe^^
Owner
Posts: 831
Joined: Fri May 12, 2006 10:20 pm
Location: Tennessee, USA
Contact:

Post by SpiKe^^ »

Maybe....

Code: Select all

proc del_in_fd {nick uhost hand chan text} {

  ##variable who added nick to delete (dont use list commands on strings!)##
  set who [lindex [split $text] 0]
  ##nicknames variable  stored values ​​I said above & split to a list.##
  set nicknames [split [getinfo]]

  ##Compare who (nick to delete) with the value of the variable nicknames above.##
  set i [lsearch -exact -nocase $nicknames $who]

  if {$i == -1} {

    putmsg $canal_admin "$who not found in the database!"
    return 0
  } else {

    ##Delete index containing the nick in the list & join list to string.###
    set nicknames [join [lreplace $nicknames $i $i]]

    

    ###for debuggin purpose###
    putmsg $canal_admin "value nicknames: $nicknames"
    #####HERE PROBLEM######

  }
}

SpiKe^^

Get BogusTrivia 2.06.4.7 at www.mytclscripts.com
or visit the New Tcl Acrhive at www.tclarchive.org
.
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

Thanks SpiKe^^ worked perfectly. :D

If you want to share the full code with my modifications notified me that I'll post here (is in Spanish). :D
If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks :)
Post Reply