Applescript failing even though variables are equal
Your x values are strings when the value is less than ten (because you use string concatenation to prepend a zero), however they are plain numbers when the value is greater than or equal to ten.

Your y values are always strings.

So, for values less than ten, your final comparison compares a string to a string; this works as expected. For higher values, the comparison will always yield false because it is comparing a number to a string.



If you want exact string matches, then you can use use this comparison code:

if y is not equal to x as string then …




If you only care about numeric values, then you could remove the zero-prefixing code and use this comparison:

if y as number is not equal to x then …


Equivalently, you could do the coercion when you fetch the y value:

set y to (do shell script "sed -n '" &amp; x &amp; "p' ~/Desktop/numlist.txt") as number


The parentheses are necessary there because do shell script has its own optional as parameter that is not the same as normal coercion.