Why doesn't this return the intended value

Agreed. I’m sure there is a way to do what you want without global variables. An example key_encoding_array would definitely help us help you…

Can you check if this does the same thing as your working version?

function rebuilding_message(encoded_string, key_encoding_array)
    decoded_string = ""
    while !isempty(encoded_string)
        for i in key_encoding_array
            if i[2] == encoded_string[1:min(i[3], end)]
                encoded_string = @view encoded_string[(i[3]+1):end]
                decoded_string *= i[1]
            end
        end
    end
    return decoded_string
end