|
Joined: Sep 2007
Posts: 109
Vogon poet
|
OP
Vogon poet
Joined: Sep 2007
Posts: 109 |
Identifiers that could be used to sort the letters of a word in alphabetical order? Example: /alphabetical hello
ehllo
/alphabetical house
ehosu
|
|
|
|
Joined: Aug 2004
Posts: 7,168
Hoopy frood
|
Hoopy frood
Joined: Aug 2004
Posts: 7,168 |
I wasn't able to find anything in the official help file, or in the special help file I have that contains undocumented commands/identifiers, but this code worked for the testing I did. It's a bit more complex than necessary, but it allows for multiple words. alias alphabetical {
if !$1 { .notice $nick You must specify one or more words }
else {
var %a = 1, %b = $0, %sorted
while %a <= %b {
var %alpha1 = $($+($,%a),2), %c = 1, %d = $len(%alpha1), %alpha2
while %c <= %d {
%alpha2 = $+(%alpha2,$mid(%alpha1,%c,1),$chr(32))
inc %c
}
%alpha1 = $sorttok(%alpha2,32)
%sorted = $+(%sorted,$chr(32),$remove(%alpha1,32))
inc %a
}
$iif($isid,return,echo -a) $1- sorted is %sorted
}
|
|
|
|
Joined: Jan 2003
Posts: 2,125
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,125 |
but this code worked for the testing I did. What was your test exactly? Did you try it on the OP's original 'hello' example? The code doesn't work, and there are two reasons for that: 1. %alpha2 = $+(%alpha2,$mid(%alpha1,%c,1),$chr(32)) doesn't work because you cannot append a single space to a variable. This works however: %alpha2 = %alpha2 $mid(%alpha1,%c,1)2. $remove(%alpha1,32) should be $remove(%alpha1,$chr(32))
|
|
|
|
Joined: Jul 2007
Posts: 1,124
Hoopy frood
|
Hoopy frood
Joined: Jul 2007
Posts: 1,124 |
%sorted = $+(%sorted,$chr(32),$remove(%alpha1,$chr(32)))
There's a closing parenthesis missing.
|
|
|
|
Joined: Aug 2004
Posts: 7,168
Hoopy frood
|
Hoopy frood
Joined: Aug 2004
Posts: 7,168 |
1) %alpha2 = $+(%alpha2,$mid(%alpha1,%c,1),$chr(32)) is not appending a single space to a variable, but rather appending a single character and a space to a variable. Normally I would've preferred to use $addtok for something like this, but for this specific situation $addtok would not work, as duplicate letters (such as the 2 l's in hello) would only show as one letter.
|
|
|
|
Joined: Aug 2005
Posts: 1,052
Hoopy frood
|
Hoopy frood
Joined: Aug 2005
Posts: 1,052 |
Here is a smaller efficient way to sort words weither your typing one or several words and want a break down in alphabetical order to see how many a's b's c's are in order or whatever you want to do Using the left right routine properly
alias alphabetical {
var %x = 1
unset %nob
while (%x <= $len($1-)) {
%nob = %nob $left($right($1-,%x),1)
inc %x
}
.timer 1 1 //echo -a $!sorttok(%nob,32)
}
|
|
|
|
Joined: Oct 2005
Posts: 1,671
Hoopy frood
|
Hoopy frood
Joined: Oct 2005
Posts: 1,671 |
Here is a sort routine that uses the Selection Sort method, rather than mIRC's $sorttok. It does not support multiple words; you can call it from within a loop to sort multiple words.
alias sort {
tokenize 32 $$1-
var %s = $1
var %hname = $+(asort.,$ticks)
if ($hget(%hname)) hfree %hname
var %i = 0, %ii = $len(%s)
while (%i < %ii) {
inc %i
hadd -m %hname $+(s.,%i) $asc($mid(%s,%i,1))
}
var %x, %i = 0, %ii = $calc($len(%s) - 1)
while (%i < %ii) {
inc %i
%x = %i
var %j = %i, %jj = $calc(%ii + 1)
while (%j < %jj) {
inc %j
if ($hget(%hname,$+(s.,%x)) >= $hget(%hname,$+(s.,%j))) { %x = %j }
}
var %t = $hget(%hname,$+(s.,%i))
hadd %hname $+(s.,%i) $hget(%hname,$+(s.,%x))
hadd %hname $+(s.,%x) %t
}
var %ss, %i = 0, %ii = $len(%s)
while (%i < %ii) {
inc %i
%ss = $+(%ss,$chr($hget(%hname,$+(s.,%i))))
}
$iif($isid,return,echo -a) %ss
hfree %hname
}
/sort <word> $sort(<word>) The code has to use hash tables, because mIRC doesn't support arrays. -genius_at_work
|
|
|
|
Joined: Jan 2003
Posts: 2,125
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,125 |
Not sure what you're talking about, there's no parentheses missing from anywhere. I didn't post the entire variable assignment, only the modification to $remove().
|
|
|
|
Joined: Jan 2003
Posts: 2,125
Hoopy frood
|
Hoopy frood
Joined: Jan 2003
Posts: 2,125 |
It doesn't matter if you're appending a space alone or anything followed by a space; as long as there's a space at the end of the string you're appending (and the previous character in that string is not a space as well), the space will be chopped off.
|
|
|
|
Joined: Jul 2007
Posts: 1,124
Hoopy frood
|
Hoopy frood
Joined: Jul 2007
Posts: 1,124 |
When I replaced RussellA's $remove(%alpha1,32) with your suggestion $remove(%alpha1,$chr(32)), I received an error of invalid format on line 12. Upon putting an extra parenthesis at the end of $remove(%alpha1,$chr(32))), it resolved it.
Last edited by Tomao; 06/10/08 05:48 PM.
|
|
|
|
|