mIRC Homepage
Posted By: Kaepez Comparing 6 variables efficiently. - 23/02/05 06:47 AM
I'm currently working on something for a friend, which involves user input from up to 6 different people. However, I need a way to efficiently compare the 6 inputs, and make sure they're unique. I know how to store the inputs into say, variables %1, %2, %3, %4, %5, and %6, but I am unaware if there is any easier way to compare them besides using a large number of if then else branches.

Could I get some help? Thanks.
Posted By: Mpdreamz Re: Comparing 6 variables efficiently. - 23/02/05 08:22 AM
if ($ifif(%1 == %2 && %2 == %3 && %3 == %4 && %4 == %5,$true,$false)) {
??
if ($iif(%1 == %2 && %2 == %3 && %3 == %4 && %4 == %5,$true,$false)) {
??
Posted By: captain_comic Re: Comparing 6 variables efficiently. - 23/02/05 09:55 AM
Sorry, I don't believe in this solution. Best way I think is sorting the variables and then comparing each two subsequent variables, like:

var %sortvar = $sorttok(%var1 %var2 %var3 %var4 %var5 %var6, 32)
var %counter = 1
var %unique = $true
while (%counter < 6) {
if ($gettok(%sortvar, %counter, 32) == $gettok(%sortvar, $calc( %counter + 1 ), 32) {
set %unique $false
}
inc %counter
}
if (%unique) {
... code for unique variables
}
else {
... code for equal variables
}

Btw, $sorttok( .. variables .. , 32, n) will perform a numeric sort, without ", n" it's alfabetic
Posted By: SladeKraven Re: Comparing 6 variables efficiently. - 23/02/05 10:08 AM
And if you had read the initial post you'd see that the original poster wanted something similar to what Ricky posted.
Short, effective.

Edit: It wont work anyway, invalid use of parentheses.
Code:
if ($gettok(%sortvar, %counter, 32) == $gettok(%sortvar, $calc( %counter + 1 ), 32)[color:red])[/color] {


I wont load it in and test for more errors, it was just something I spotted off hand.
Posted By: DaveC Re: Comparing 6 variables efficiently. - 23/02/05 10:48 AM
if ($numtok($addtok($addtok($addtok($addtok($addtok(%1,%2,13),%3,13),%4,13),%5,13),%6,13),13) != 6) { oh dear one or more inputs was the same )

Addtok well not add a already existent token so , i just add all the tokens and if there not totalling 6 then one was the same.

Im using 13 as the token seperator as $cr is hardly ever in someone input smile
Posted By: captain_comic Re: Comparing 6 variables efficiently. - 23/02/05 10:55 AM
Maybe my English is not good enough, but when I read that the original poster wants to be sure that ALL variables are unique, I come to the conclusion that none of them may be equal to any other one. Ricky's solution won't work for that, it only checks whether ALL variables are equal to each other (true), or at LEAST one of them is not (false). By the way, when I try Ricky's solution, I get incorrect format message. And sorry for my typing mistake *bow*
Posted By: captain_comic Re: Comparing 6 variables efficiently. - 23/02/05 10:57 AM
Quote:
Addtok well not add a already existent token so , i just add all the tokens and if there not totalling 6 then one was the same.

Very clever!!
Posted By: qwerty Re: Comparing 6 variables efficiently. - 23/02/05 12:02 PM
if ($regex($+(%1,$lf,%2,$lf,%3,$lf,%4,$lf,%5,$lf,%6),/^(.+)\n(?:.+\n)*\1$/m)) { echo oh dear }

Somewhat shorter, although you can save a char in yours by converting != 6 to < 6 :tongue:
Posted By: FiberOPtics Re: Comparing 6 variables efficiently. - 23/02/05 05:45 PM
if $regex($sorttok(%1 %2 %3 %4 %5 %6,32),/(\S+) \1/) { echo -a Not unique }

I suppose it's better using $lf because that won't appear in a string in IRC, which is better as a delimiter than a space, since a space can be part of a single input.

I guess if it's certain that %1 %2 ... are always single words, you could use what I posted, and preferably the $lf way if a single input can contain multiple words, because then part of one input might match a part of another input.

Greets
Posted By: qwerty Re: Comparing 6 variables efficiently. - 23/02/05 08:10 PM
Yeah, if the input cannot contain spaces one can think of various short ways. In yours, by the way, you should check what's before (\S+) and after \1, to avoid errors with strings like "air battle lemon". The regex should look something like this:
/(?:^| )(\S+) \1(?:$| )/
Posted By: FiberOPtics Re: Comparing 6 variables efficiently. - 23/02/05 08:24 PM
Yep.
whya re we giving me credit for the solution that i just made a correction in
Posted By: Kaepez Re: Comparing 6 variables efficiently. - 24/02/05 12:04 AM
Thanks for all the help. It works nicely.

I'm beginning to wonder if there's a simple way to determine not only if all six variables are not unique, but also which two, if any, are identical.

Any ideas? If not, I'm sure I can find a way around it.
Posted By: captain_comic Re: Comparing 6 variables efficiently. - 24/02/05 02:36 AM
Now make it work for an infinite number of input variables.. :tongue:
Posted By: SladeKraven Re: Comparing 6 variables efficiently. - 24/02/05 03:37 AM
If it's an infinite number, the poor guy/girl will be there years. With their brain fried like an egg. shocked
Posted By: Kelder Re: Comparing 6 variables efficiently. - 24/02/05 04:20 PM
Here you go smile
Use either //echo -s $alldifferent(a,b,c,d,e,f,,g) or /alldifferent a b c d e f g

Code:
alias alldifferent {
  ; $1 is first entry, $2 second etc
  ; only limitation is line length (about 900 characters)
  var %txt, %n = 1, %t = 0
  var %delim = $asc(Ø)
  var %r = $iif($isid,return,echo -s)
  while ( %n &lt;= $0) {
    if ([ $ [ $+ [ $v1 ] ] ] == $null) {
      ; ignore all $null values
      inc %n    
      continue 
    }
    var %txt = $addtok(%txt,$v1,%delim)
    inc %t
    if ($numtok(%txt,%delim) &lt; %t) {
      var %tok1 = $findtok(%txt, $ [ $+ [ %n ] ] ,%delim)
      var %tok2 = %n
      var %contents = $ [ $+ [ %n ] ]
      ; should give same result: 
      ; var %contents = $gettok(%txt,%tok1,%delim)
      %r Parameter %tok1 is the same as %tok2 $+(; contents are &lt;,%contents,&gt;.)
      return $false
    }
    inc %n
  }
  %r All parameters are unique...
  return $true
}
Posted By: DaveC Re: Comparing 6 variables efficiently. - 24/02/05 08:33 PM
Points to raise.

1) Since two $null strings are the same as each other i felt this missed a potential matching.
2) Any string with asc216 "Ø" would have been corrupting the test, you could fix that with var %ts = 255 | while ((%ts) && ($pos($1-,$chr(%ts),1))) { dec %ts } | var %delim = %ts
3) %tok1 would hold an invalid token number if a $null parameter had preceeded it

4) I also didnt like how it returned textual results but thats personal valour,

I wrote this check4match alias
Code:
alias c4m {
  var %i = 1
  while (%i &lt; $0) {
    var %j = %i
    while (%j &lt; $0) {
      inc %j
      if ( $ [ $+ [ %i ] ] == $ [ $+ [ %j ] ] ) {
        $iif($isid,.) echo -sq * c4m $true %i %j $ [ $+ [ %i ] ]
        return $true %i %j $ [ $+ [ %i ] ]
      }
    }
    inc %i
  }
  $iif($isid,.) echo -sq * c4m $false
  return $false
}

* a case-sensitive version could exist using c4mcs and the IF having ===

//c4m a b c d | c4m a b c b | .c4m a b c d | echo $c4m(a,,b,c,b) | echo $c4m(a,,b,,c,d) | echo $c4m(1,b,c,d)
* c4m $false
* c4m $true 2 4 b
<no reply for 3rd example>
$true 3 5 b
$true 2 4
$false
Posted By: FiberOPtics Re: Comparing 6 variables efficiently. - 24/02/05 09:54 PM
Usage: $chkdup(string1,string2,...,stringN)

Returns 0 if there are no duplicates, N if there are N duplicates.

Code:
alias chkdup {
  scon -r var % $+ a = $* $!!cr ,%b = $!addtok(%b,%a,13)
  return $calc($0 - $numtok(%b,13))
}

Strings may not contain $cr or an escaped version of $cr.
It takes $null's into consideration, so $chkdup(,,) returns 2.
Posted By: Kelder Re: Comparing 6 variables efficiently. - 25/02/05 05:21 PM
1) I deliberately ignored all $null values. It just depends on the situation (his requirements) I guess... Apparently $0 also counts trailing null arguments in $id's, but not trailing spaces/null identifiers in /commands. So, that means there's no way to check for doubles when using the /command form.

A simple implementation of a script detecting $nulls would be alias alldifferent return $false $calc($0 + 1) $calc($0 + 2) smile

2) Yes, but your solution will still do the same when all ascii codes are used. :tongue: I did put it up front to let the OP choose the character of his liking.
3) It would be the %tok1 'th not-null argument, agreed that it's not $ $+ %tok1
4) I hope the OP will change it too, just wanted to return everything somewhat readably
Posted By: DaveC Re: Comparing 6 variables efficiently. - 26/02/05 12:19 AM
1) Its not that $0 looses the trailing $null arguements from the end, in fact it "looses" them all, but its not really loosing them its simply they are not passed since <space><null><space> is parsed by mirc into <space>
2) I know if it couldnt find a free asc it would crap out but then it would have crapped anyway as theres no token seperator frown
© mIRC Discussion Forums