mIRC Home    About    Download    Register    News    Help

Print Thread
#204893 05/10/08 03:16 AM
Joined: Sep 2007
Posts: 109
K
kwell Offline OP
Vogon poet
OP Offline
Vogon poet
K
Joined: Sep 2007
Posts: 109
Identifiers that could be used to sort the letters of a word in alphabetical order?
Example:
Code:
/alphabetical hello 
ehllo

/alphabetical house
ehosu



kwell #204894 05/10/08 04:34 AM
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
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.
Code:
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,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Quote:
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))


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
qwerty #204902 05/10/08 05:57 PM
Joined: Jul 2007
Posts: 1,129
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
%sorted = $+(%sorted,$chr(32),$remove(%alpha1,$chr(32)))

There's a closing parenthesis missing.

qwerty #204904 05/10/08 06:41 PM
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
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
L
Hoopy frood
Offline
Hoopy frood
L
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

Code:
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)
}


Code:
if $reality > $fiction { set %sanity Sane }
Else { echo -a *voices* }
kwell #204917 05/10/08 10:55 PM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
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.

Code:

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

Tomao #204937 06/10/08 02:51 PM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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().


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
qwerty #204940 06/10/08 05:38 PM
Joined: Jul 2007
Posts: 1,129
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
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.

Link Copied to Clipboard