mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Nov 2008
Posts: 8
D
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
D
Joined: Nov 2008
Posts: 8
Ive been trying for a few days to create a $swaptok() identifier
and i keep coming up with an error where it moves the token to the second specified slot
syntax: $swaptok( text, token1, token2, C )
it is sposta swap token1 and token2 in text using c as the seperator
for instance $swaptok( 1 2 3 4 5 6 7 8 9, 1, 2, 32 ) SHOULD return 2 1 3 4 5 6 7 8 9 and when $swaptok( 2 1 3 4 5 6 7 8 9, 1, 2, 32 ) should return 1 2 3 4 5 6 7 8 9 however when attempting to run $swaptok the 2nd time it doesnt switch frown i know what my error is but cant seem to solve it

alias swaptok {
if ( $0 < 4 ) {
echo 2,1 error: $swaptok() requires 4 or more inputs
halt
}
set %dlktemp1 $numtok( $1-, 32 )
if ( %dlktemp1 == 4 ) set %dlktemp2 $1
if ( %dlktemp1 > 4 ) //set %dlktemp2 $gettok( $1-, 1- $+ $calc( %dlktemp1 - 3 ), 32 )
if ( %dlktemp1 == 4 ) set %dlktemp3 $2-
if ( %dlktemp1 > 4 ) //set %dlktemp3 $gettok( $1-, $calc( %dlktemp1 - 2 ) $+ - $+ %dlktemp1, 32 )
set %dlktemp4 $gettok( %dlktemp2, $gettok( %dlktemp3, 2, 32 ), $gettok( %dlktemp3, 3, 32 ) )
set %dlktemp5 $gettok( %dlktemp2, $gettok( %dlktemp3, 1, 32 ), $gettok( %dlktemp3, 3, 32 ) )
set %dlktemp2 $puttok( %dlktemp2, %dlktemp5, $gettok( %dlktemp3, 2, 32 ), $gettok( %dlktemp3, 3, 32 ) )
set %dlktemp2 $puttok( %dlktemp2, %dlktemp4, $gettok( %dlktemp3, 1, 32 ), $gettok( %dlktemp3, 3, 32 ) )
return %dlktemp2
}

Note: all variables are left intact for debugging until i fix it

Joined: Jan 2007
Posts: 1,156
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
This requires that $2 is the first item in the list.

For instance, you want to swap 2 and 5 in the text "1 2 3 4 5". You would have to say $swaptok(1 2 3 4 5,2,5,32)

If you wanted then to swap 5 with 2, you would have to say $swaptok(1 5 3 4 2,5,2,32).

Code:
alias swaptok { var %swp2 = $reptok($1,$2,$3,1,$4), %swp3 = $reptok(%swp2,$3,$2,2,$4) | return %swp3 }


I will continue to write an alias to swap anything. It's not as difficult as you are making it.



Here you go ...
Code:
alias swaptok {
  if (!$4) { echo -a Swaptok Error: Not Enough Parameters | return }
  var %swptok = $numtok($1,$4), %swpx, %swp2, %swp3 | while (%swptok) {
    %swpx = $gettok($1,%swptok,$4)
    if (%swpx == $2) { %swp2 = %swptok }
    elseif (%swpx == $3)  { %swp3 = %swptok }
    dec %swptok
  }
  %swpx = $puttok($puttok($1,$2,%swp3,$4),$3,%swp2,$4) | return %swpx
}

Last edited by DJ_Sol; 14/11/08 01:14 AM.
Joined: Nov 2008
Posts: 8
D
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
D
Joined: Nov 2008
Posts: 8
my original intention however, was to eventually stick it all onto one line therefor not using variables and this was just my attempt to separate it all so i could stick it back together in logistical order but, thank you for the solution smile

Last edited by darklord_knaag; 14/11/08 03:21 AM.
Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
While the solution you were given could be put all onto one line, I, for one, wouldn't want to have to try to debug it if something went wrong.

If you're thinking that a multi-line script will take longer to process than a single line script, that may be the case, but the time difference (if any) is going to be so small, that I'd have to wonder if you'd see the difference using $ticks to check the running time. (ie: less than one millisecond)

Joined: Nov 2008
Posts: 8
D
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
D
Joined: Nov 2008
Posts: 8
actually it has nothing to do with time as i stated above i dont want to use variables

Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
I can't see how this could be done without using variables, or at least, not until Khaled implements this with the rest of the token identifiers (which means that a Feature suggestion has to be made).

The variables used in the code are all local variables, so they're only set for the amount of time that it takes for the code to run.

Would you mind explaining why you don't want to use variables, when they make the code so much easier to read and follow?

Joined: Nov 2008
Posts: 8
D
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
D
Joined: Nov 2008
Posts: 8
well why would be so i dont have a bunch of extra variables bogging up my variables tab bc right now im sitting at close to 500 >.> because alot of scripts i use dont unset the variables after use so all i really want is to keep that clean for anyone and everything that would be using that identifier

Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Let me introduce you to the /var command

/help /var

Use this rather than /set to create a local variable which is automatically destroyed after the alias completes.

-genius_at_work

Joined: Nov 2008
Posts: 8
D
Nutrimatic drinks dispenser
OP Offline
Nutrimatic drinks dispenser
D
Joined: Nov 2008
Posts: 8
OOOH somehow i skipped over /var >.> really makes me feel like a noob. It must be a sad thing then that im the best mirc script writer on the irc i use.


Link Copied to Clipboard