| View previous topic :: View next topic |
| Author |
Message |
ultralord Master

Joined: 06 Nov 2006 Posts: 255
|
Posted: Sat Jan 10, 2009 3:32 pm Post subject: linsert and something else |
|
|
i can insert one variable into one file like this:
set g_stats [linsert $g_stats end $admin]
set fileio [open /home/ul/public_html/ircstats/g_stats.txt "w"]
puts $fileio $g_stats
flush $fileio
close $fileio
can i remove one variable from the file like linsert other command? i search but nothing..
thanks in adv |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Jan 10, 2009 3:56 pm Post subject: |
|
|
To remove a list item from a list, use the lreplace command with no "element" parameters. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
ultralord Master

Joined: 06 Nov 2006 Posts: 255
|
Posted: Sat Jan 10, 2009 4:16 pm Post subject: |
|
|
like that?
set g_stats [lreplace $g_stats end $admin]
set fileio [open /home/ul/public_html/ircstats/g_stats.txt "w"]
puts $fileio $g_stats
flush $fileio
close $fileio
thanks for the help i want to remove $admin value from the $g_stats file |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Jan 10, 2009 5:09 pm Post subject: |
|
|
Not quite...
lreplace expects a list, a start index, an end index, and optionally one or more elements to replace the range with (no elements obviously means don't add anything, simply remove the range). _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
ultralord Master

Joined: 06 Nov 2006 Posts: 255
|
Posted: Sat Jan 10, 2009 6:14 pm Post subject: |
|
|
can you make me one expample to underestand? or to make that "easy" script for me but you must move it to request this topic.. and i think i underestand how it works if i saw something
i want to remove one $admin from one specific .txt file only that.. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sat Jan 10, 2009 7:17 pm Post subject: |
|
|
A good start would be to read the manpage for lsearch and lreplace, as these comes with some rather good examples. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
ultralord Master

Joined: 06 Nov 2006 Posts: 255
|
Posted: Sun Jan 11, 2009 5:26 pm Post subject: |
|
|
set stats2 [lsearch -exact $g_stats $admin ]
set g_stats2 [lreplace $g_stats $admin 0 0]
like that?> replace the $admin with 0 ?
thanks |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Sun Jan 11, 2009 7:15 pm Post subject: |
|
|
The command lsearch returns the index of the first match or -1 if no match is found
set idx [lsearch $testlist $pattern]
So, in the above example, the value of the variable named idx has been assigned the index of the first match of $pattern in $testlist OR -1 if $pattern is not found in $testlist.
To remove the first occurance of $pattern from the list :-
if {$idx != -1} {
set testlist [lreplace $testlist $idx $idx]
}
Or, to replace the first occurance of $pattern in $testlist with 0 :-
if {$idx != -1} {
set testlist [lreplace $testlist $idx $idx 0]
}
If you are having trouble understanding the commands as described in the reference documents (perhaps English is not your first language) then I recommend you install a Tcl distro on your desktop and use the provided Tclsh to experiment with core Tcl statements. Alternatively, you could enable Tcl commands in your bot's partyline (this has the benefit of understanding both core Tcl and Eggdrop Tcl). These are examples from my bot's partyline :-
[23:12] <arfer> .tcl set testlist {one two three four}
[23:12] <Baal> Tcl: one two three four
[23:12] <arfer> .tcl set pattern three
[23:12] <Baal> Tcl: three
[23:13] <arfer> .tcl set idx [lsearch $testlist $pattern]
[23:13] <Baal> Tcl: 2
[23:13] <arfer> .tcl set testlist [lreplace $testlist $idx $idx 0]
[23:13] <Baal> Tcl: one two 0 four
Experiment! It is unlikely you will break anything. |
|
| Back to top |
|
 |
ultralord Master

Joined: 06 Nov 2006 Posts: 255
|
Posted: Sun Jan 11, 2009 7:34 pm Post subject: |
|
|
nice thnx for all explanation .. i underestand somethings better..
so for my script i must put that:
testlist is my path for .txt and pattern is my $admin
set idx [lsearch $testlist $pattern]
if {$idx != -1} {
set testlist [lreplace $testlist $idx $idx]
}
because i want to remove the $admin from the txt file..
variable $admin have one nick on his value
thanks for all help |
|
| Back to top |
|
 |
ultralord Master

Joined: 06 Nov 2006 Posts: 255
|
Posted: Mon Jan 12, 2009 8:52 pm Post subject: |
|
|
| so? my previus post is mistake? |
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Mon Jan 12, 2009 8:57 pm Post subject: |
|
|
| ultralord wrote: | | so? my previus post is mistake? |
| Code: | WRONG...
set stats2 [lsearch -exact $g_stats $admin ]
set g_stats2 [lreplace $g_stats $admin 0 0]
CORRECT..
set stats2 [lsearch -exact $g_stats $admin ]
set g_stats2 [lreplace $g_stats $stats2 $stats2] |
lsearch is the lindex position, so use $stats2 to lreplace it...
If $stats2 happens to be negative (the lsearch failed) then there will be an error setting g_stats2. We aren't testing for failed conditions before setting that variable as we are assuming it will never fail because you haven't given any indication as to why it ever would. This isn't rocket science. If you make a mistake it doesn't kill anyone or cost millions of dollars. It just means you learn, try to experiment on your own at times...
Also, for clarification, the above assumes a proper tcl list is present. So the biggest question of all becomes something only you can answer for us, "Is $g_stats a list or a string?" If it's a list, how are the elements composed? You might need to use "-glob" with wildcards within your lsearching. If it's a string then there are more problems with your script than you've shown. Perhaps revealing more of the code would yield the answer?  _________________ speechles' eggdrop tcl archive |
|
| Back to top |
|
 |
ultralord Master

Joined: 06 Nov 2006 Posts: 255
|
Posted: Tue Jan 13, 2009 3:07 am Post subject: |
|
|
yes thnx and i underestand is so big fault and nothing costs :/
also look what i want.. i have this
set g_stats [linsert $g_stats end $admin]
set fileio [open /home/ul/public_html/ircstats/g_stats.txt "w"]
puts $fileio $g_stats
flush $fileio
close $fileio
with that i put $admin on $g_stats .. $admin = Ultralord (had value one nick)
on then the nick is added to a list on /home/ul/public_html/ircstats/g_stats.txt like nick1 nick2 nick3 nick4.. then i want to make one lsearch/lreplace to remove the $admin from that list on txt when i want.. on my script.. sorry for my english ;/ do you underestand better now? |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Tue Jan 13, 2009 11:14 am Post subject: |
|
|
Ultralord: In response to your query a few posts back;
- testelist: This would be the variable holding the list of nicknames, not the file. I believe you called this g_stat.
- pattern: this would be a pattern to find the list item (nickname) to be removed. lsearch uses "glob"-style matching by default, allowing the use of wildcards (*?) aswell as character ranger ([a-z]). Matching is case sensitive. Assuming the same layout as in your other function, this would be admin.
In your case, it might be a better idea to use the "exact"-style matching of lsearch. This would be accomplished by altering the lsearch-command like below (once again adjusting variable names):
| Code: | | set idx [lsearch -exact $testelist $pattern] |
_________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Tue Jan 13, 2009 11:56 am Post subject: |
|
|
I'm not sure I agree entirely with the last posting. If there are no wildcards or character ranges in the pattern, which seems to be the case here, then the pattern is specific in which case the -glob AND -exact options are superfluous. An exact match IS required without actually specifying a -exact option.
[15:52] <@arfer> % return [lsearch {one two three} th]
[15:52] <@Baal> -1
[15:52] <@arfer> % return [lsearch {one two three} ree]
[15:52] <@Baal> -1
[15:52] <@arfer> % return [lsearch {one two three} threes]
[15:52] <@Baal> -1
[15:52] <@arfer> % return [lsearch {one two three} three]
[15:52] <@Baal> 2 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Tue Jan 13, 2009 2:19 pm Post subject: |
|
|
The issue at hand would be in cases where the nickname would contain \[]. The point of -exact is to explicitly disallow wildcard and range matching done in a glob-style matching.
Regardless of what kind of matching-mechanism is chosen, the pattern must match the whole entity, a partial match is never sufficient.
| arfer wrote: | | An exact match IS required without actually specifying a -exact option. |
This is incorrect, as the default mechanism of lsearch is glob-style matching. For exact-style matching, you explicitly need to add the -exact option. The examples posted does not support your statement. Remember that a trivial glob-pattern would appear to be an exact-match, yet with non-trivial data you will see a difference in matching... _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
|