Creating an array of hashes and having issues

A

Alpha Blue

I have an array that contains 36 objects. The composition of those
objects includes a date, a team, and a time. It is being parsed from
another site.

[0] = Sept 2
[1] = vsSouthern Miss
[2] = 7:30 PM ET
[3] = Sept 9
[4] = @Florida
[5] = 7:30 PM ET
etc.

Every 3 objects should be combined into a specific hash row.

[0][:date] = Sept 2
[0][:team] = vsSouthern Miss
[0][:time] = 7:30 PM ET
[1][:date] = Sept 9
[1][:team] = @Florida
[1][:time] = 7:30 PM ET
etc.

I'm having trouble accomplishing this. Each schedule might have more
than 36 objects in the array, but the divisor will always be 3 in terms
of what type of information is presented. So, a team might have an
array of 39 objects or 42 objects but in the end, the objects are a
date, team, and a time.

I've tried numerous things, even adding a new class method:

class Array
def to_spec_hash(other)
Hash[ *(0...self.size()).inject([]) { |arr, ix| arr.push(self[ix],
other[ix]) } ]
end
end

and then trying to use:

%W{ date team time }.to_spec_hash( %W{ value1 value2 value3 etc. } )

but I'd have to iterate over date team and time according to how many
rows of data divided by 3 would be and the same for the values. Again,
I know it's sloppy.

I'm sure there's a much faster and a cleaner way of accomplishing this
but I'm lacking sleep and therefore not thinking as clearly.

Any help would be appreciated.
 
R

Rick DeNatale

I have an array that contains 36 objects. =A0The composition of those
objects includes a date, a team, and a time. =A0It is being parsed from
another site.

[0] =3D Sept 2
[1] =3D vsSouthern Miss
[2] =3D 7:30 PM ET
[3] =3D Sept 9
[4] =3D @Florida
[5] =3D 7:30 PM ET
etc.

Every 3 objects should be combined into a specific hash row.

[0][:date] =3D Sept 2
[0][:team] =3D vsSouthern Miss
[0][:time] =3D 7:30 PM ET
[1][:date] =3D Sept 9
[1][:team] =3D @Florida
[1][:time] =3D 7:30 PM ET
etc.

I'm having trouble accomplishing this. =A0Each schedule might have more
than 36 objects in the array, but the divisor will always be 3 in terms
of what type of information is presented. =A0So, a team might have an
array of 39 objects or 42 objects but in the end, the objects are a
date, team, and a time.

I've tried numerous things, even adding a new class method:

class Array
=A0def to_spec_hash(other)
=A0 =A0Hash[ *(0...self.size()).inject([]) { |arr, ix| arr.push(self[ix],
other[ix]) } ]
=A0end
end

and then trying to use:

%W{ date team time }.to_spec_hash( %W{ value1 value2 value3 etc. } )

but I'd have to iterate over date team and time according to how many
rows of data divided by 3 would be and the same for the values. =A0Again,
I know it's sloppy.

I'm sure there's a much faster and a cleaner way of accomplishing this
but I'm lacking sleep and therefore not thinking as clearly.

Any help would be appreciated.

Is this what you want?

input =3D [
"Sept 2",
"vsSouthern Miss",
"7:30 PM ET" ,
"Sept 9" ,
"@Florida" ,
"7:30 PM ET"
]

input.extend Enumerable

p input.enum_for:)each_slice, 3).map { |date, team, time|
{:date =3D> date, :team =3D> team, :time =3D> time}

}

[{:date=3D>"Sept 2", :team=3D>"vsSouthern Miss", :time=3D>"7:30 PM ET"},
{:date=3D>"Sept 9", :team=3D>"@Florida", :time=3D>"7:30 PM ET"}]


This is written for 1.8.6, for 1.8.7 or 1.9.x Enumerable is built in
in 1.9 and you can get the enumerator by calling each_slice without a
block.

p (input.each_slice(3)).map { |date, team, time|
{:date =3D> date, :team =3D> team, :time =3D> time}

}

In any event if it were me I'd make a Game class, or at least use an
OpenStruct instead of a Hash
--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
A

Alpha Blue

Thanks Rick - this example did work but I believe your second idea was a
better one. I'll look into that instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top