Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array of Hashes in an array of hashes - Complicated!

Reply
Thread Tools

Array of Hashes in an array of hashes - Complicated!

 
 
Matt Brooks
Guest
Posts: n/a
 
      09-15-2009
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
each time)
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 #
times
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: <nameofbitstructobject>.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
something like
> signal_data[signal_index][:num_times_repeated_array][repeated_field_index][arameter_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
this?
> puts signal_data[signal_index]['num_times_repeated_array'][1]['parameter_name']




*****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
know!!!

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
num_times_next_two_repeat.




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.
-Matt
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Matt Brooks
Guest
Posts: n/a
 
      09-15-2009
> Therefore to save the first repeating field "I think" it would be
> something like
>> signal_data[signal_index][:num_times_repeated_array][repeated_field_index][arameter_name] = parameter_value


Two things, I meant for the above to save parameter_name for now, later
on, i want it to save the parameter_value to a hash key of the
parameter_name...
>> signal_data[signal_index][:num_times_repeated_array][repeated_field_index][arameter_name] = parameter_name



Also I guess I forgot to declare the hashes for the second array:
>num_times_repeated_array[repeated_field_index] = Hash.new


Then I could save stuff to it???

> signal_data[signal_index][:num_times_repeated_array][repeated_field_index][arameter_name] = parameter_name


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
John W Higgins
Guest
Posts: n/a
 
      09-15-2009
[Note: parts of this message were removed to make it a legal post.]

Morning Matt,

On Tue, Sep 15, 2009 at 8:59 AM, Matt Brooks <> wrote:

> 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 don't have any specific solution here for you other then to suggest that
you might be very much limiting yourself by thinking only in terms of Arrays
and Hashes. This sounds like something where a nice object would be more
appropriate. It might take more "legwork" to develop a good layout here but
it would seem that you would be much better off.


> num_signals #how many times this entire structure repeats(varies
> each time)
> 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 #
> times
> 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
>
>

Something like a Signal object with id, freq, and bandwidth as
attr_accessors along with parameters as a hash stored within the object
might be a nice starting point.

You could then have an array of Signal objects that would store the
individual signals.

John

 
Reply With Quote
 
Matt Brooks
Guest
Posts: n/a
 
      09-15-2009
Hi John,
Thanks for the input, how might I create a variable number of objects
with different object names though?

if the following while loop happens the same object is created over and
over again. I loose say the other 30 signal objects i have made. As I
was saying before how would you increment a variable name or object name
in this implementation?

while index < num_signals
sig_1 = Signal.new
....
....
end


Also, I would need a way to overwrite and kill all the objects every 10
seconds or so, when all the signals update with different information.
Is that possible?

How might I keep track of all the signal objects created and all their
unique names and then kill them, so that I can then make a variable
number of new signal objects that contain a different number of
variables possibly, and definitely different data.

Thanks!
Matt
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
John W Higgins
Guest
Posts: n/a
 
      09-15-2009
[Note: parts of this message were removed to make it a legal post.]

Matt,

On Tue, Sep 15, 2009 at 11:15 AM, Matt Brooks <> wrote:

> Hi John,
>

...


> while index < num_signals
> sig_1 = Signal.new
> ....
> ....
> end
>


I would create an array of the objects. It's not a matter of throwing away
arrays or hashes but better organization of your data into objects rather
then layers of data in hashes and array will just make things easier.

For example

sig_array = Array.new
while index < num_signals
sig = Signal.new
sig.id = ...
...
sig_array << sig
end


> Also, I would need a way to overwrite and kill all the objects every 10
> seconds or so, when all the signals update with different information.
> Is that possible?
>


Just clear the array when you want to drop everything

sig_array.clear

John

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      09-15-2009
Matt Brooks wrote:
> Hi John,
> Thanks for the input, how might I create a variable number of objects
> with different object names though?
>
> if the following while loop happens the same object is created over and
> over again. I loose say the other 30 signal objects i have made. As I
> was saying before how would you increment a variable name or object name
> in this implementation?
>
> while index < num_signals
> sig_1 = Signal.new
> ....
> ....
> end
>


You wouldn't.

signals = []

while index < num_signals
signals << Signal.new
end


>
> Also, I would need a way to overwrite and kill all the objects every 10
> seconds or so, when all the signals update with different information.
> Is that possible?
>


signals = []

> How might I keep track of all the signal objects created and all their
> unique names and then kill them, so that I can then make a variable
> number of new signal objects that contain a different number of
> variables possibly, and definitely different data.
>
> Thanks!
> Matt


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Aldric Giacomoni
Guest
Posts: n/a
 
      09-15-2009
+1 on object creation

There's a Ruby tutorial somewhere on the interblag about creating a
mini-adventure game. It begins like this:

def Thing
attr_accessor :name, :description
end

And everything starts from there.

def Weapon < Thing
end

def Sword < Weapon
end

etc etc. You probably don't need that much breaking down, but it seems
you do need to break it down some
The basic way to think about it is : if I can't explain it in three
sentences or less, I have to break it down.

If you REALLY REALLY REALLY wanted to create some kind of iterating
name, you could always do things like
iterator = 0
array = []
while iterator < 30
array << "a#{iterator}".to_sym
iterator += 1
end

But that's really dirty...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      09-15-2009
Hi --

On Wed, 16 Sep 2009, Aldric Giacomoni wrote:

> +1 on object creation
>
> There's a Ruby tutorial somewhere on the interblag about creating a
> mini-adventure game. It begins like this:
>
> def Thing
> attr_accessor :name, :description
> end
>
> And everything starts from there.
>
> def Weapon < Thing
> end
>
> def Sword < Weapon
> end
>
> etc etc. You probably don't need that much breaking down, but it seems
> you do need to break it down some
> The basic way to think about it is : if I can't explain it in three
> sentences or less, I have to break it down.
>
> If you REALLY REALLY REALLY wanted to create some kind of iterating
> name, you could always do things like
> iterator = 0
> array = []
> while iterator < 30
> array << "a#{iterator}".to_sym
> iterator += 1
> end
>
> But that's really dirty...


array = (0...30).map {|i| :"a#{i}" }

(Probably irrelevant to the thread but I couldn't resist


David

--
David A. Black, Director
Ruby Power and Light, LLC (http://www.rubypal.com)
Ruby/Rails training, consulting, mentoring, code review
Book: The Well-Grounded Rubyist (http://www.manning.com/black2)

 
Reply With Quote
 
Matt Brooks
Guest
Posts: n/a
 
      09-15-2009
Thank you for everyone's input, I am thinking about it all right now,
and hope to have a combined solution tomorrow.

I have to figure out how to make a Signal Object, i can't see how I am
going to get around having this messed up hash problem even if I make a
signal object. because of the repeating fields within the data.

I think having objects only changes the fact of instead of having an
array of hashes at my top level, i have an array of objects that have
hashes inside of them anyway... I have hundreds of variables for each
signal, and I can't make this signal object have 200 local variables, I
think it would be dirty to type
attr_reader: for all the hundreds variables...

Plus then only some of them get defined each time...

Am I thinking about this correctly? i have seen that tutorial, i think
actually in my book here by Peter Cooper or at least something similar,
although I am not seeing how it relates fully.

Thanks!!!
Matt
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Rob Biedenharn
Guest
Posts: n/a
 
      09-15-2009
On Sep 15, 2009, at 5:44 PM, Matt Brooks wrote:
> Thank you for everyone's input, I am thinking about it all right now,
> and hope to have a combined solution tomorrow.
>
> I have to figure out how to make a Signal Object, i can't see how I am
> going to get around having this messed up hash problem even if I
> make a
> signal object. because of the repeating fields within the data.
>
> I think having objects only changes the fact of instead of having an
> array of hashes at my top level, i have an array of objects that have
> hashes inside of them anyway... I have hundreds of variables for each
> signal, and I can't make this signal object have 200 local
> variables, I
> think it would be dirty to type
> attr_reader: for all the hundreds variables...


Consider what a Hash is: a special mapping of keys to values with the
restriction that a key can only appear once. Why not represent the
data not as a {'key'=>'value'} but as ['key','value']? Unless the
performance optimization of the Hash lookup is truly important. You
can then use things like Array#assoc to get the pair back.

>
> Plus then only some of them get defined each time...
>
> Am I thinking about this correctly? i have seen that tutorial, i
> think
> actually in my book here by Peter Cooper or at least something
> similar,
> although I am not seeing how it relates fully.
>
> Thanks!!!
> Matt
> --



You get the ordering (which is what comes to my mind when you say
"repeating fields") of an array, but your have pairs rather than
simple scalar values.

-Rob

Rob Biedenharn http://agileconsultingllc.com



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
On Hashes - How the hashes printing works? Neela megha shyam Chivukula Ruby 4 05-28-2009 10:56 AM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57