You don't really need the inner while loop in your code, because it is going to execute an un-necessary number of times.
You really only need to check to make sure the current character in %word exists in %random (use $pos or $count). Then if it does, use $pos to find the character and remove it from %random (using a combination of $left and $right).
The reason your code wasn't working was because the statement:
if ($left(%word,%i) isin %random)
Is comparing %word in increasing sizes to %random.
If %word = mirc, %random = rmci
The first iteration of the while loop, it'll work fine --> if (m isin rmci)
The 2nd iteration of the while loop is where it breaks --> if (mi isin rmci)
As you can see, it'll fail because you're trying to compare 2 characters instead of 1. You need to only compare 1 character at a time.
I've modified your code a little, and below it I'm putting explanations of what I did.
alias randomGen {
set %theMatch
var %word = $1
var %random = $2
var %i = 1
while (%i <= $len(%word)) {
var %char $mid(%word,$eval(%i), 1)
if (%char isin %random) {
set %theMatch %thematch $+ $mid(%word,$eval(%i), 1)
}
inc %i
}
echo -a %theMatch
}
1. while (%i <= $len(%word))
I changed the while loop to use <= instead of <, and eliminated the $calc you had there.
2. var %char $mid(%word,$eval(%i), 1)
$mid(%word,$eval(%i), 1) will take 1 characater from %word, starting at position %i. It will save this char in %char
3. if (%char isin %random)
If the character is in %random, this will succeed.
4. set %theMatch %thematch $+ %char
Adds the character to %theMatch
Now, the things I left out.. You need to remove the character from %random, or else you'll get false matches. for example, if %word = radar, and %random = ard, the code above will still say you have a match because technically %random has all of the characters that %word has. Also, after the while loop you'll want to make sure that %random is empty if you care about an exact match.