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.

Need help with list sorting (lsort).

Old posts that have not been replied to for several years.
Locked
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Need help with list sorting (lsort).

Post by awyeah »

Can anyone here help me out on a basic thing.

I am wanting to sort a list alphabetically.

Example:

Code: Select all

set mylist "benny alan ryan sarah rosey arnold adrian jessica"
Which should give an output like sorting them aplhabetically:

Code: Select all

set mylist "adrian alan arnold benny jessica rosey ryan sarah"
So the names are sorted alphabetically in the list.

And one more, question.

What is the difference between 'lsearch' and 'string match'?
They both give the same type of matching procedure is it?
I am not clear with lsearch, I know about string match though.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

'lsort' should do the job.
User avatar
Papillon
Owner
Posts: 724
Joined: Fri Feb 15, 2002 8:00 pm
Location: *.no

Post by Papillon »

to make the lsort sort alphabetically use the -dictionary command.

Code: Select all

lsort -dict $mylist
lsearch and string match both use the same sort of matching as default, but lsearch can also do matching with the re_syntax (used by regexp and regsub)
string commands are designed to be used on... strings :)
lsearch on lists
string match just returns 1 or 0 on match or no match
while lsearch returns -1 on no match or the index of first hit on match (unless you set -all or -inline or something like that to the option)
read more about it here

... hope it makes abit more sence now ;)
Elen sila lúmenn' omentielvo
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Geee... Thanx... :lol: for the detailed info Papillion I appreciate it.
I'm more clearler in this concept now, I'll try to lookup and read
the manual and other references on these topics as well. 8)
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

One more small question, regarding this topic.

The lsort -dictionary sorts using the dictionary rule alphabetically from A to Z.

When I do [chanlist $chan] I get a random output as well, then I use lsort to sort it out.

But how can I make it in a reverse way (the other way around). For example from "z**** y**** x**** ....... c**** b***** a****), rather than using "a b c .... x y z". The inverse!

How would the lsort be used to sort indexes using the dictionary in the inverse way alphabetically? From Z to A (reverse sequence)


****
Another small question. I have a script detecting the total number of bans in a banlist. Which can be found by [llength [chanbans $chan]]. However I am not getting the correct result. Always I get 1 less ban. If there are 5 bans in the channel's banlist, I get 4 bans. If there is 1 ban in the channel's banlist I get a null result.

I don't know what is wrong. This is quite a simple procedure? Is it because maybe of the lindex $var 0, index 0? in the list? Please help me.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Try lsort -decreasing $list

llength [chanbans $chan] seems to work just fine, gives me the correct number of bans everytime..
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Um.... ok so we can use -increasing -decreasing as well... I only thought -dictionary.

Yeah, I don't know whats the problem with chanbans can't get the accurate listning hmmm. I am using DALnet, maybe some problem with the IRCd or could be the raw display or something. I'll try to check it out.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

maybe you should take a look at http://www.tcl.tk/man/tcl8.4/TclCmd/lsort.htm
aVote page back online!
Check out the most popular voting script for eggdrop bots.

Join the metal tavern!
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Na its... okay I got the hang of lsort and its switches.

I have looked at the manual. I have it all on my hddisk
for reference, thats the first thing I did before I tried
to do tcl scripting.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Hi, I have a small question related to lists.
How can we delete an element from a list?

Searching through the tcl manual with relation to lists, I've found a function which I think would do it.

Code: Select all

set newlist [lreplace $mylist 0 end "$botnick" ""]
Would this be ok? Will it delete and element named '$botnick' (the bots nickname) from the list $mylist. If not, please let me know, if any other function is availiable to remove/delete a particular element from a list.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Hmm.. I don't know about the code I posted in my previous thread on this topic. But I came up with another one:

Code: Select all

set newlist [list]
foreach user $mylist {
 if {([lsearch -exact [string tolower $user] [string tolower $botnick]] == -1)} {
 lappend newlist $user
 }
}
I think this would give the same purpose, if not right?
It will make a new list, exempting the variable I have defined :-?
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
arcane
Master
Posts: 280
Joined: Thu Jan 30, 2003 9:18 am
Location: Germany
Contact:

Post by arcane »

edit: mmh... not sure about removing multiple items, but this is the code is use for deleting single items:

Code: Select all

set newlist [lreplace $list $pos $pos]
where $list is your existing list and $pos is the position (you get by using "lsearch $item") of the item you want to delete.
aVote page back online!
Check out the most popular voting script for eggdrop bots.

Join the metal tavern!
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Umm....... thanks I'll give it a go. :)
Though I use this one, works fine for me. :mrgreen:

Code: Select all

set newlist [list]
foreach user $mylist {
 if {(![string equal -nocase $botnick $user])} {
 lappend newlist $user
 }
}
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Locked