How did you verify that that removed all spaces from $word?
I suggest that, as an exercise, you write a short program that removes all whitespace from a list of "interesting" words in an array. This will teach you how to investigate and verify your assumptions about your data and your program.