Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2159
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Simple array.each do |x| question |
| Date | 2011-04-02 13:32 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <2b98c82b7bbb2ec7c8c0e99b7d0efe84@ruby-forum.com> (permalink) |
| References | (2 earlier) <9d19e7d38f2f3b6ab99b3150717a307c@ruby-forum.com> <AANLkTinUfc7WfoXosbPVFG5P7YBgCF8RVAzhVUN9FAAm@mail.gmail.com> <6f6bb08b25ceb72409f83615448a82b6@ruby-forum.com> <20110401225432.GB86915@guilt.hydra> <4ed2ae7acfe8cdab6fbb0fbff47e3e65@ruby-forum.com> |
Kyle X. wrote in post #990532:
>
> So once I flatten the array it would become - > [0,1,2,3,4,5,6,7,8] from
> [[0,1,2],[3,4,5],[6,7,8]] correct?
>
Yes, and you can always test that. If you use:
p some_array
ruby will output the array structure.
> This would then allow me to all in one .to_f, yes? Would this be
> possible to do without flattening, or is it not possible with arrays in
> an array?
>
You wouldn't want to flatten in that case. Instead you can use some
nested map() calls:
data = [[0,1,2],[3,4,5],[6,7,8]]
transformed_data = data.map do |arr|
arr.map do |int|
int.to_f
end
end
p transformed_data
--output:--
[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]
map() sends each element of the data array to the block parameter arr.
Because each element of the data array is a sub-array, I named the block
parameter 'arr' to indicate that. Now that you have a reference to the
sub-array inside the outer block, you can apply another map() to the
subarray to do the actual transformation. The inner map() block returns
a new array that has transformed one of the data sub-arrays to floats.
And the outer map stuffs each of those returned-transformed-arrays into
a new array--producing the final result.
--
Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-03-31 17:26 -0500
Re: Simple array.each do |x| question Roger Braun <roger@rogerbraun.net> - 2011-03-31 17:35 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-03-31 17:59 -0500
Re: Simple array.each do |x| question Gunther Diemant <g.diemant@gmx.net> - 2011-03-31 18:06 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-03-31 18:30 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-01 14:29 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-01 15:31 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-02 01:39 -0500
Re: Simple array.each do |x| question 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-02 13:32 -0500
Re: Simple array.each do |x| question 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-02 13:58 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-04 13:55 -0500
Re: Simple array.each do |x| question "Kyle X." <haebooty@yahoo.com> - 2011-04-06 00:57 -0500
csiph-web