If you can print the variable immediately before you leave the block and see the value you expect, then print the variable immediately after you leave the block and see a different value, that strongly suggests a "masking variable" situation like the ones discussed by 1nickt here and sundialsvc4 here.
IOW: You have a lexical variable defined within a statement block (a lexical scope) that has the same name as a variable defined outside the block. You assign to and print the variable inside the statement block, but that variable goes away when execution leaves the block, at which time you are left with the outside variable and whatever value it has. I can only suggest you carefully examine your code.
Give a man a fish: <%-{-{-{-<
|