How to the output in 12x8 format

Hi folks,

I have an array containing 96 elements. I want to print the array in
12x8 format. Any comments?

Thanks in advance,

Li

On 10/19/06, Li Chen [email protected] wrote:

Hi folks,

I have an array containing 96 elements. I want to print the array in
12x8 format. Any comments?

Thanks in advance,

Li

require ‘enumerator’

ary = (1…96).to_a
ary.each_slice(8) {|slice| puts slice.inspect}

[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, 58, 59, 60, 61, 62, 63, 64]
[65, 66, 67, 68, 69, 70, 71, 72]
[73, 74, 75, 76, 77, 78, 79, 80]
[81, 82, 83, 84, 85, 86, 87, 88]
[89, 90, 91, 92, 93, 94, 95, 96]

Blessings,
TwP

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Li Chen wrote:
| I have an array containing 96 elements. I want to print the array
| in 12x8 format. Any comments?

Old school way (Beware! Ternary operator ahead!):

a = (1…96).to_a.reverse
a.each_with_index{|x,i|print x,i%12==11?"\n":"\t"}

Idea:

Iterate x over all elements of a
. print x
. is it value number eleven (the twelveth) in the row?
. . if so break line
. . if not jump to next tabulator
. condition ends
iteration ends

Jupp
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFFN/b0rhv7B2zGV08RAnGUAJ0YcmrBhwZzORu0zxzZ7ObuxV1YpwCg4GAk
dc6aobYd2tKBxQzJ8Wq4Fpk=
=k96z
-----END PGP SIGNATURE-----

On 10/19/06, Li Chen [email protected] wrote:

[89, 90, 91, 92, 93, 94, 95, 96]

Hi,

Why should I write the code :require ‘enumerator’?

Is enumerator a module or class?

The ‘each_slice’ method is defined in the Enumerable module, but
portions this module are not included by default. Requiring
enumerator adds some extra methods to the Enumerable objects –
each_slice being one of those methods.

Take a look at the Pragmatic book for some more info. It’s on page
672 of the real book. I could not find any reference to each_slice in
the online version.

The rdoc documents aren’t too helpful with this one. They just list
all the Enumerable methods. It gives the impression that all of them
are right there in the Array class (which is not the case).

I’m done rambling now. Going home to see the little kiddo and wife :slight_smile:

Blessings,
TwP

require ‘enumerator’

ary = (1…96).to_a
ary.each_slice(8) {|slice| puts slice.inspect}

[1, 2, 3, 4, 5, 6, 7, 8]

[89, 90, 91, 92, 93, 94, 95, 96]

Hi,

Why should I write the code :require ‘enumerator’?

Is enumerator a module or class?

Thanks,

Li

On Oct 19, 4:35 pm, Li Chen [email protected] wrote:

Hi folks,

I have an array containing 96 elements. I want to print the array in
12x8 format. Any comments?

Thanks in advance,

Li

Hi.

The slicing method is neat.
However, not having the book, this is what i came upon, supposing you’d
want to keep the string and not only to print it.

class Array
def print_format(width)
str = ‘’
from_index = 0
while !self[from_index].nil?
str << self[from_index, width].inspect + “\n”
from_index += width
end
str
end
end

n = (1…96).to_a
puts n.print_format(8)

Conclusion: I want my book!
Regards.

On 10/19/06, Tim P. [email protected] wrote:

require ‘enumerator’
[49, 50, 51, 52, 53, 54, 55, 56]
[57, 58, 59, 60, 61, 62, 63, 64]
[65, 66, 67, 68, 69, 70, 71, 72]
[73, 74, 75, 76, 77, 78, 79, 80]
[81, 82, 83, 84, 85, 86, 87, 88]
[89, 90, 91, 92, 93, 94, 95, 96]

Or as an alternative:

ary = (1…8).to_a
(0…7).each {|i| p ary[i*8, 8] }

One advantage of Tim’s suggestion of using each_slice is that the data
doesn’t really need to be an array, it can be another class which
mixes in Enumerable.

require ‘enumerator’
not_really_array = (1…96)
not_really_array.each_slice(8) { |slice| p slice }

[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, 58, 59, 60, 61, 62, 63, 64]
[65, 66, 67, 68, 69, 70, 71, 72]
[73, 74, 75, 76, 77, 78, 79, 80]
[81, 82, 83, 84, 85, 86, 87, 88]
[89, 90, 91, 92, 93, 94, 95, 96]


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs