Hi @Sukera, I agree the problem must be residing in the for-loop and if-statement. Not sure where the problem lies but seems that it is there.
to whom it may concern.
I have found a way out but it is not an elegant solution. And that bothers me.
In essence I do not know why the for loop keeps going eventhough it had (emphasis on āhadā) a break in all of the previous incarnation ( and please spare me the comments on return rebuilding_message
it does not work probably for the same reasonā¦).
The for loop does not stop when I want it to and thus breaks down the very value I want to return.
Hence the fact that the print(decoded_string)
but the return of the same value does not.
It is now a working huffman compression algorithm, that can be code reviewed by a third party. btw I hate using Global variables, because it shows to me a lack of understanding of the programmers code
ā¦the ironyā¦
function rebuilding_message(encoded_string,key_encoding_array,decoded_string)
for i in key_encoding_array
if isempty(encoded_string) global decoded_answer = decoded_string
elseif i[2] == try encoded_string[1:i[3]] catch e encoded_string end
rebuilding_message(SubString(encoded_string,(i[3]+1)),key_encoding_array,string(decoded_string,i[1]))
end
end
return decoded_answer
end
I mean, you didnāt really give us much to go off of An example call with example arguments & the expected output would be helpful for debugging (this is called a āMWEā, minimal (non-)working example), which is what @goerch asked for, nothing more. Folks in this forum may not be familiar with Huffman encoding, so they may not know what should be changed in the algorithm in the first place, so such examples are tremendously helpful. In part thatās also why I suggested a logic error in your if
, because thereās not really another place where your code could be going wrong.
Do you maybe have a working implementation in another language youāre maybe porting your code from that we could look at as well, to find the difference?
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
Just want to point out that neither of your posted versions had a break in the for loop, both of them had an if statement with the recursive call, but after the recursive call returned all of them would have continued to run the for loop until completion. And the value returned from the recursive call is not stored so it would be lost.
If you want to stop the for loop and return the value there are multiple different ways, the simplest would likely be the return
statement suggested by others, but if this really doesnāt work for you there are other ways to exit the loop. Though a bit more context would greatly help us in helping you, for example an input to run it with together what your expected output is.
Iām quite familiar with Huffman encoding but I still canāt give any useful advice without a concrete example of input data and expected result.