mIRC Home    About    Download    Register    News    Help

Print Thread
Page 1 of 2 1 2
K
Kaepez
Kaepez
K
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.

Joined: Apr 2004
Posts: 755
M
Hoopy frood
Offline
Hoopy frood
M
Joined: Apr 2004
Posts: 755
if ($ifif(%1 == %2 && %2 == %3 && %3 == %4 && %4 == %5,$true,$false)) {
??

R
ricky_knuckles
ricky_knuckles
R
if ($iif(%1 == %2 && %2 == %3 && %3 == %4 && %4 == %5,$true,$false)) {
??

C
captain_comic
captain_comic
C
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

Joined: Dec 2002
Posts: 3,534
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,534
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.

D
DaveC
DaveC
D
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

C
captain_comic
captain_comic
C
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*

C
captain_comic
captain_comic
C
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!!

Joined: Jan 2003
Posts: 2,125
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,125
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:

Joined: Feb 2004
Posts: 2,013
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Feb 2004
Posts: 2,013
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

Joined: Jan 2003
Posts: 2,125
Q
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,125
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(?:$| )/

Joined: Feb 2004
Posts: 2,013
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Feb 2004
Posts: 2,013
Yep.

R
ricky_knuckles
ricky_knuckles
R
whya re we giving me credit for the solution that i just made a correction in

K
Kaepez
Kaepez
K
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.

C
captain_comic
captain_comic
C
Now make it work for an infinite number of input variables.. :tongue:

Joined: Dec 2002
Posts: 3,534
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,534
If it's an infinite number, the poor guy/girl will be there years. With their brain fried like an egg. shocked

Joined: Apr 2003
Posts: 701
K
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
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
}

D
DaveC
DaveC
D
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

Joined: Feb 2004
Posts: 2,013
F
Hoopy frood
Offline
Hoopy frood
F
Joined: Feb 2004
Posts: 2,013
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.

Joined: Apr 2003
Posts: 701
K
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
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

Page 1 of 2 1 2

Link Copied to Clipboard