[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

format problem

Li Chen

12/10/2006 3:45:00 AM

Hi all,

I have a 1D array containing 96 elements. I change it
into a 2D array then print it out: each line/row is an
element with whatever columns I want. The problem is
that if the column number is < 10 I can print out
the format that I want. If the column number is >=10 I
still can print the results but the format is changed.
Also I use object#inspect method to check the data
structure: both of them are an 2D array. If the 1D
array is defined as _1D_array=1..96 then change it to
a 2D array no such problem happens. Any comments?


Thanks,

Li




#script

_1D_array.each_slice(s) do |i|
_2D.array<<I
end

pp _2D.arry


## if column is 8
[[2294.4, 3481.2, 2716.7, 1672.2, 1135.3, 2103.5,
591.1, 648.5, 603.0],
?
[11900.4, 10823.3, 10090.5, 3271.5, 4560.7, 3617.6,
1815.7, 855.3, 915.4],
[583.3, 601.1, 565.6, 459.2, 349.3, 358.0, 351.1,
340.2, 488.2],
[13.0, 14.1, 14.1, 16.2, 16.1, 27.1]]

##if column is 12

[[2294.4,
3481.2,
2716.7,
1672.2,
1135.3,
2103.5,
591.1,
648.5,
603.0,
477.2,
264.1,
626.5],
?
[459.2,
349.3,
358.0,
351.1,
340.2,
488.2,
13.0,
14.1,
14.1,
16.2,
16.1,
27.1]]






____________________________________________________________________________________
Cheap talk?
Check out Yahoo! Messenger's low PC-to-Phone call rates.
http://voice...

1 Answer

Paul Lutus

12/10/2006 7:41:00 AM

0

chen li wrote:

> Hi all,
>
> I have a 1D array containing 96 elements. I change it
> into a 2D array then print it out: each line/row is an
> element with whatever columns I want. The problem is
> that if the column number is < 10 I can print out
> the format that I want. If the column number is >=10 I
> still can print the results but the format is changed.
> Also I use object#inspect method to check the data
> structure: both of them are an 2D array. If the 1D
> array is defined as _1D_array=1..96 then change it to
> a 2D array no such problem happens. Any comments?

Just one. What are the data, and what result do you want?

Show examples of the data that originate in your one dimensional, 96 element
array, and show the desired output formatting.

Here is an example of what you might want, but this can only be a guess
under the circumstances:

-------------------------------------

#!/usr/bin/ruby -w

array = []

1.upto(96) { array << rand(256) }

max_cols = 10

col = 0

array.each do |item|
printf("%4d",item)
puts if (col += 1) % max_cols == 0
end

puts

-------------------------------------

Example output:

88 227 250 145 115 255 93 4 91 5
138 120 26 89 148 225 29 95 71 232
99 166 99 250 23 2 205 147 176 221
228 33 156 97 74 241 7 252 236 85
120 150 46 227 175 23 250 186 151 154
9 245 239 164 242 6 89 180 86 239
27 141 161 251 38 7 190 195 9 106
127 220 112 18 86 247 82 82 122 101
109 250 70 189 224 181 87 116 238 58
124 118 99 18 188 53

--
Paul Lutus
http://www.ara...