I have an unique problem that I can’t solve. I am sorry this is long,
but the details are needed to solve it I think.
The reason I have to use an array of hashes that sometimes has an array
of a hashes in it is as follows.
I have a variably repeating block of binary data with variable repeating
fields within it; which I was previously simply decoding using
bit-structs and also using unpack to get variably repeating fields, and
then just printing the data to the screen. Now, I need to save all
those values into hash keys, so they are accessible later, after they
have been printed to the screen.
**Problem: The entire variable length data structure repeats a variable
amount of times(known before-hand in a variable at the beginning),
inside this data structure are some variables occurring variable amounts
of times(also known right before the repeating of the fields occurs).
For a simple outline of variables I have that need to be stored instead
of simply displaying/printing them as I parse the incoming data… of
course this masks some other complications I will address later, but the
short example should suffice.
num_signals #how many times this entire structure repeats(varies
signal_id #regular variable easily saved in hash
signal_freq #regular variable easily saved in hash
num_times_next_two_repeat #next two fields repeat as a pair this #
parameter_name #again this repeats with the below as a pair N times
parameter_value #again this repeats with the above as a pair N times
signal_bandwidth #regular variable easily saved in hash
***Quick summary of how I was printing/displaying this complicated mess
to the screen: All these variables are available to me using bit-struct
fields and then using unpack to loop through the cases where there are a
variable number of times a pair of variables repeat within the data. I
previously would just print the regualar fields using the inspect method
of bitstruct to print each individual variable, but is easily accessible
using this: .nameofbitstructfield
Then I would display an appended string of the variable fields by using
unpack to unpack the parameter_name and parameter_value and append it to
a string for displaying later, then unpack the next parameter_name and
parameter_value and append it to the same string, etc… then finally
after all variable repeating fields were done, I would display the
string to the screen. This worked fine for just displaying to the user,
now I need them saved uniquely(they repeat and have the same name, can’t
have two hash keys with same name, ie. part of problem).
So, now I want each value to be in a hash, and each key name has to be
unique. I WISH I WISH I could simply increment a hash key symbol name
(I don’t think it is possible). I wish I could just add to my hash as I
have been doing for the other non-repeating fields and just increment
the hash key names like parameter_name0 and parameter_value0, then
parameter_name1 and parameter_value1. But since this occurs a variable
amount of times I can’t hardcode the variable names.
Since I don’t think that is possible, I thought I could create, another
array that holds hashes of the 2 repeating fields, ie. array.length
would equal num_times_next_two_repeat.
I have previously created the top array called, signal_data:
signal_data = Array.new(num_signals, 0)
Then before each new iteration for each signal the following runs:
signal_data[signal_index] = Hash.new
Therefore to save the first repeating field “I think” it would be
signal_data[signal_index][:num_times_repeated_array][repeated_field_index][:parameter_name] = parameter_value
****Couple problems That I don’t understand how to fix:
1st of all, should this work at all?
How do I declare the second array?
Is it like this?
:num_times_repeated_array = Array.new(num_times_next_two_repeat, 0)
I am confused about the colon used in the hash key and the colon at the
beginning of the name of the array itself, does the name of the array
have the colon?
Would you be able to get to a 2nd repetition of the repeated field like
*****NEXT PROBLEM if the above step is able to work:
So if that indeed that accesses that repeated field, here is the next
problem, how can I get that repeated field to be the hash key of the
next repeated field, which would be the hash value of that key? Crazy I
I want this embedded second array to only hold one key and value pair
per array index, ie. the parameter_name to be the hash key and the
parameter_value to be the hash value for each index in the array. And
the length of that array of course would be = to that variable
Thank you very much for whatever help you can offer, I know it is long.
But I can not figure it out. I’ll be waiting, wish I could say
patiently but probably not, haha.