mIRC Homepage

help identifiers

Posted By: kwell

help identifiers - 05/10/08 03:16 AM

Identifiers that could be used to sort the letters of a word in alphabetical order?
/alphabetical hello 

/alphabetical house

Posted By: RusselB

Re: help identifiers - 05/10/08 04:34 AM

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
Posted By: qwerty

Re: help identifiers - 05/10/08 10:19 AM

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))
Posted By: Tomao

Re: help identifiers - 05/10/08 05:57 PM

%sorted = $+(%sorted,$chr(32),$remove(%alpha1,$chr(32)))

There's a closing parenthesis missing.
Posted By: RusselB

Re: help identifiers - 05/10/08 06:41 PM

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.
Posted By: Lpfix5

Re: help identifiers - 05/10/08 07:52 PM

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)
Posted By: genius_at_work

Re: help identifiers - 05/10/08 10:55 PM

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>

The code has to use hash tables, because mIRC doesn't support arrays.

Posted By: qwerty

Re: help identifiers - 06/10/08 02:51 PM

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().
Posted By: qwerty

Re: help identifiers - 06/10/08 02:52 PM

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.
Posted By: Tomao

Re: help identifiers - 06/10/08 05:38 PM

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.
© 2022 mIRC Discussion Forums