A few things I think are wrong with your solution:
The use of the next is incorrect because the 'Tag' line will be looked for again since we are going to the top of the while loop.
You are missing a '~' on the $value line. Also, could you explain what that '..' is used for in that context in the if condition?