mIRC Home    About    Download    Register    News    Help

Print Thread
#33492 01/07/03 08:09 PM
Joined: Feb 2003
Posts: 143
N
naki Offline OP
Vogon poet
OP Offline
Vogon poet
N
Joined: Feb 2003
Posts: 143
Yes I made this so it reverse the text going through.
Like $rtext(hi there) would become ereht ih, but for some reason it doesnt keep the spaces. Why?

Code:
alias rtext { 
  var %#, %ns
  %# = $len($1-)
  while (%# >= 1) {
    %ns = %ns $+ $mid($1-,%#,1)
    dec %# 1
  }
  return %ns
}


We don't just write the scripts, we put them to the test! (ScriptBusters)
#33493 01/07/03 08:31 PM
Joined: Dec 2002
Posts: 169
J
Vogon poet
Offline
Vogon poet
J
Joined: Dec 2002
Posts: 169
check out Hammer's example

You cannot add a blank space to the end of a variable. It will get chopped off. His example uses $replace() to swap out the spaces for another character and then swapping it back right before it returns the result.

#33494 02/07/03 12:49 AM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
Imho using replace is a stupid way to do it, especially for reversing, it makes no sense at all. It's slow, and it's ugly. Not to insult Hammer or anything, but isn't:
Code:
alias reverse {
  var %txt
  var %i = 1
  while (%i <= $len($1-)) {
    %txt = $mid($1-,%i,1) $+ %txt
    inc %i
  }
  return %txt
}

A lot cleaner than using all that replace stuff?

#33495 02/07/03 12:55 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
You cannot add a space to the end of a variable but you can add to the beginning wink
Code:
alias rev {
  var %i = 1, %a
  while $mid($1,%i,1) != $null {
    %a = $ifmatch $+ %a
    inc %i
  }
  return %a
}


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#33496 02/07/03 01:16 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Ah you posted a similar solution while I was previewing mine.

Btw, not to insult you or anything, but letting mirc repeatedly evaluate $len($1-), instead of setting %len or something to it and use %len in the while condition, is even slower than using $replace() just twice, especially for large strings :tongue: Hammer's $replace() is unnecessary though, since I just noticed that he actually uses the same method: adding each char to the beginning of the variable.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#33497 02/07/03 02:36 PM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
Yeah you're right, however there is another problem with $replace. He replaces spaces with char 127. What happens if the text I want to reverse happens to contain $chr(127)? Ok yes, it is unlikely you'll have text with the backspace(?) character in it, but it is still possible.

#33498 02/07/03 05:00 PM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
I agree, any $replace solution can never be good enough (perhaps $lf or $cr instead of $chr(127) would be a better idea, since at least cr/lf cannot be in IRC text). And although Hammer's method is actually pointless (since he's adding to the beginning of the var anyway, where the space-related issue doesn't apply), I do understand the reason he used it and I can't call it stupid: he probably knew that you cannot add a space to the end of a var but he didn't know/remember that you can add it to the beginning. And I don't blame him; remembering all mirc quirks is nearly impossible.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#33499 02/07/03 06:58 PM
Joined: Dec 2002
Posts: 169
J
Vogon poet
Offline
Vogon poet
J
Joined: Dec 2002
Posts: 169
Ahh, I stand corrected. I too forgot that you can put spaces in the front of a variable. It's definately cleaner and you don't need to worry about the character already existing in $1-.

* Jerk walks away in shame blush

#33500 02/07/03 07:21 PM
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
Here is my Super Sexy reverse alias, using &binvars. grin

Code:
rev {
  bset -t &bin 1 $$1-
  var %i = 0, %n = $bvar(&bin,0)
  bset &bout %n 0
  WHILE %n {
    inc %i
    bcopy &bout %i &bin %n 1
    dec %n
  }
  return $bvar(&bout,1,%i).text
}

I should note that I used /bcopy instead of /bset w/ $bvar() in the loop, because it would seem to be faster. I also zero-pad &bout when I initialize it, so it doesn't have to keep allocating more memory.

Btw, I'm appalled at the way codemastr used $len() in a WHILE condition. That's just irresponsible!

- Raccoon


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
#33501 02/07/03 09:05 PM
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
I found this to be a bit faster. Originally I created a text-var one working on the same principle (double substitution per iteration) but then when I came to post you'd put yours up which was a tiny bit faster. So 15mins later and it now uses binvars wink.

Code:
rev {
  bset -t &t 1 $$1-
  var %i = 1, %j = $bvar(&t,0), %t = %j
  bset -t &r %t 0
  while %i < %j {
    bcopy &r %i &t %j 1
    bcopy &r %j &t %i 1
    inc %i
    dec %j
  }
  if %i == %j { bcopy &r %i &t %i 1 }
  return $bvar(&r,1,%t).text
}


Spelling mistakes, grammatical errors, and stupid comments are intentional.
#33502 02/07/03 10:01 PM
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
(God, I hate how it's impossible to paste code from here into mirc, without breaking it into multiple lines by hand)

Very interesting approach. At first I couldn't figure out why you use two /bcopy's... but you're right, it is faster. Your method cuts the number of loop iterations in half by using both the low and high numbers until they meet in the middle.

While at first this seems slightly underhanded, as everyone knows inline code runs faster at the expense of neatness, you are exploiting a valid resource I overlooked. That's a pretty darn neat method. smile

Here's what mine looks like again, but using your method, so the two can be compared side by side. (not to steal your code, but you have to really see the simplicity of it. I only changed 1 line and added 1 line)

Code:
rev2 {
  bset -t &bin 1 $$1-
  var %i = 0, %n = $bvar(&bin,0)
  bset &bout %n 0
  WHILE %i < %n {             | ; <-- Modified this line.
    inc %i
    bcopy &bout %i &bin %n 1
    bcopy &bout %n &bin %i 1  | ; <-- Added this line
    dec %n
  }
  return $bvar(&bout,1,%i).text
}

This is exactly as fast as your version, which takes about 1/3rd the time of my original example (when reversing 500 characters). Very nice dude!


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
#33503 02/07/03 11:07 PM
Joined: Jul 2003
Posts: 32
K
Ameglian cow
Offline
Ameglian cow
K
Joined: Jul 2003
Posts: 32
thanks for the reverse text thing! I've been trying to do that forever!


KevinJr42
#33504 03/07/03 01:48 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
(God, I hate how it's impossible to paste code from here into mirc, without breaking it into multiple lines by hand)

Paste to Wordpad first, then copy from there and paste to mirc.

I like starbucks_mafia's algorithm too, very clever indeed. His implementation seems to be a tiny bit faster than your last version, probably because your while loop iterates once more (as you initiate %i to 0 and not to 1). One more while means 1 more /inc, 1 more /dec and 2 more /bcopy's. Otoh, starbucks_mafia's /if statement (after the loop) contains only 1 /bcopy. So you're exactly 1 /bcopy, 1 /dec and 1 /inc slower smile


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#33505 03/07/03 01:57 AM
Joined: Dec 2002
Posts: 1,922
O
Hoopy frood
Offline
Hoopy frood
O
Joined: Dec 2002
Posts: 1,922
It's a good time to mention that $bvar() accepts ranges in the second parameter, allowing us to easily display the whole content of a binvar of an unknown length using 1-, therefore $bvar(&bout,1,%i).text can be changed to $bvar(&bout,1-).text

And if I'm not mistaken, you were the first to introduce this method smile

#33506 03/07/03 02:26 AM
Joined: Jan 2003
Posts: 2,523
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
Yeah, I've come across quite a few pleasant surprises of this kind in mirc smile $bvar() with 1-, negative-negative ranges in token idents (although negative-positive or positive-negative ranges aren't supported yet), $nick() accepting mode symbols (not only letters), $window(*,N) allowing you to iterate through all windows (not just custom ones), to name a few.


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com
#33507 03/07/03 08:02 AM
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
nice about the 1-, that's the primary reason why I /inc'd %i the way I did, so I wouldn't have to keep an extra variable, or $calc it minus 1. any idea if using 1- is any slower than specifying a known length?


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
#33508 03/07/03 08:14 AM
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Yeah the range is a nice touch, means I don't need that stupid %t variable which was uglifying my beautiful code. In practice it's a teeny tiny bit faster in some tests I tried. $rev() has gone on a diet and lost 12 bytes! grin


Spelling mistakes, grammatical errors, and stupid comments are intentional.
#33509 03/07/03 08:26 AM
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
Any way I could make this prettier?
Code:
brot13 {
  bset -t &bin 1 $$1-
  var %a, %n = $bvar(&bin,0)
  WHILE %n {
    %a = $bvar(&bin,%n)
    if ( %a isnum 65-77 ) || ( %a isnum 97-109 ) $&
      { inc %a 13 | bset &bin %n %a }
    elseif ( %a isnum 78-90 ) || ( %a isnum 110-122 ) $&
      { dec %a 13 | bset &bin %n %a }
    dec %n
  }
  return $bvar(&bin,1-).text
}
($&'s added to fit code nicely on the screen, and should be removed for speed)

I really hate having to /bset in both conditions, but it does speed up processing if the string has non-alpha characters in it.

Think $ifmatch would be faster than %a in this instance? From my experience, %variables evaluate faster than $identifiers, even $ifmatch

- Raccoon


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
#33510 03/07/03 12:44 PM
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
OK so this code isn't beautiful, but it worked out about 25x faster than the method you were using. Plus if someone wanted to add extra characters for rotation, such as non-English alphas, they could just be appended. Again, the $&'s are just for the sake of forum readability.

Code:
rot13 {
  bset -t &r 1 $$1
  breplace &r 97 110 98 111 99 112 100 113 101 114 102 115 103 116 104 117 105 118 106 119 107 120 108 121 $&
    109 122 110 97 111 98 112 99 113 100 114 101 115 102 116 103 117 104 118 105 119 106 120 107 121 108 122 109
  breplace &r 65 78 66 79 67 80 68 81 69 82 70 83 71 84 72 85 73 86 74 87 75 88 76 89 77 90 $&
    78 65 79 66 80 67 81 68 82 69 83 70 84 71 85 72 86 73 87 74 88 75 89 76 90 77
  return $bvar(&r,1-).text
}


Spelling mistakes, grammatical errors, and stupid comments are intentional.
#33511 05/07/03 03:04 AM
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
eek! shocked


Well. At least I won lunch.
Good philosophy, see good in bad, I like!

Link Copied to Clipboard