Li Chen wrote:

> Paul Lutus wrote:
>> Does it produce a result that is different than your own code, when used
>> with 'pp'? Does it solve your problem?
> 
>> 
>>> And here is my codes with the problem:
>> 
>> What is the problem at this point? What is 'pp'? How do you export your
>> data
>> to Excel? What happens if you adopt my method instead of your own?
> 
> 
> Hi Paul,
> 
> When I run your script I need to use pp(pretty print) to get 8 rows x12
> columns format on the screen.

No, you /do/ /not/ need to do that. The formatter 'pp' is /optional/, it is
not mandatory. You do no have to use it, if it doesn't give you what you
want. Does it give you what you want? If not, stop using it.

Because you have tried to use 'pp' to get what you want, and because you are
posting again, I am guessing that 'pp' is not providing you with the result
you want. Therefore, stop trying to get 'pp' to do what you want. Try
something else.

There are any number of ways to process the data you have in your array.
Just choose one -- and tell me what it is.

> If I just use p I get all the elements on one line.

Do you want that, or do you not want that?

> But I still can see the result is a 2D array even they are 
> printed one line.

Is that what you want to happen, or not what you want to happen?

> For me I need the format in result A only. 

Which format is 'result A'? Please type in an example of what you want.

Post again, type "this is what I want" and type in what you want. When you
have done this, I will reply, showing you how to get what you want.

> 
> Here is your codes with pp(8 row x12 column format):
> 
> ###format A##
>>ruby format4.rb
> [[1256, 1165, 95, 330, 1320, 1425, 1489, 1953, 207, 1132, 1378, 1101],
>  [530, 1340, 1842, 1136, 104, 888, 378, 741, 954, 1949, 1608, 597],
>  [1379, 648, 95, 544, 1194, 1728, 1259, 691, 601, 20, 1301, 1625],
>  [652, 32, 947, 241, 248, 656, 1197, 1308, 1870, 613, 1188, 1409],
>  [680, 1294, 1842, 1947, 1467, 670, 989, 126, 1174, 964, 1868, 1875],
>  [771, 990, 687, 706, 1372, 0, 1332, 1527, 411, 1885, 658, 1903],
>  [207, 276, 71, 1097, 10, 1083, 1600, 1776, 1016, 374, 414, 472],
>  [1477, 1183, 711, 1726, 1642, 1167, 1513, 316, 28, 1285, 181, 1681]]

Is this the format you want to have? If so, you already have created it.
But, because you have posted again, I am guessing this is not what you
want.

>>Exit code: 0
> 
> ##format B##
> your code with p:(all elements in one line, 1 row x 96 column format)
>>ruby format4.rb
> [[631, 1224, 1534, 477, 642, 1915, 814, 1350, 998, 234, 1377, 1697],
> [1421, 1972, 1020, 789, 1258, 1585, 1979, 518, 1747, 1419, 169, 1067],
> [1395, 1103, 463, 1064, 1841, 1676, 1946, 1697, 1274, 153, 1125, 1415],
> [1857, 1322, 782, 778, 1704, 100, 1814, 1144, 1380, 1155, 626, 520],
> [137, 788, 1691, 1865, 1443, 20, 1699, 1595, 51, 1481, 1603, 558], [240,
> 92, 927, 635, 1910, 1806, 778, 170, 1152, 281, 1434, 1422], [1011, 143,
> 1315, 78, 1076, 828, 496, 559, 1878, 1660, 1613, 1721], [1344, 1716,
> 103, 760, 389, 1869, 716, 945, 637, 596, 1550, 752]]
>>Exit code: 0
> 
> If I run your codes with my data and if I use p I get the same format as
> that in result B.

Is this the format you want to have?

> 
> If I run your codes with my data and if I use pp I get the format C as
> follows:
> 
> ##result C
>>ruby format3.rb
> [[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]]
>>Exit code: 0

Is this the format you want to have?

> 
> Whatever codes I run the same 2D array gives rise to different print
> format and the formats also depend on the data in the 2D array. This is
> the reason why I post my question.

All that is true, you have eloquently described all the things you do not
want, but you have not said what you actually want to happen.

> 
> Again thank you very much for your time,

You are most welcome, but I wish I could figure out what you want -- that
would be more useful.

Please say what you want to happen. What are you going to do with the data?
Where does it go from here? Please post an example of what format you want
the data to have, so that it becomes useful to you.

Once you have revealed what you want your data to look like, I will post
some code to make the data look like what you say you want. But before I
can post code to make your data look like you want, you need to say what
you want.

Here are some examples of different formats:

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

#!/usr/bin/ruby -w

array = []
1.upto(96) { array << rand(2000) }

rows = 8
columns = 12

slices = []

0.upto(rows-1) do |i|
   slices << array[i * columns,columns]
end

# now that the data are in a 2D array,
# we can show different display formats:

# each row on a separate line, with commas between fields:

slices.each do |row|
   puts row.join(",")
end

# each row on a separate line, with tabs between fields:

slices.each do |row|
   puts row.join("\t")
end

# data formatted as an HTML table:

puts "<table>"
slices.each do |row|
   puts "<tr><td>" + row.join("</td><td>") + "</td>\n</tr>\n"
end
puts "</table>"

# another data format, that looks a bit like like 'p':

puts "["
slices.each do |row|
   puts "[" + row.join(",") + "],"
end
puts "]"

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

This shows that I can create any data format you might want to describe. The
problem is I cannot figure out what data format you want.

Please post a description of what you want. Say "This is what I want", and
below that, type in what you want. Don't say what you don't want any more,
I think I am clear that there are any number of formats that you do not
want.

Imagine a math class. The teacher wants the student to solve an equation.
The student has a very large blackboard on which to put his solution.

Shall the student:

(1) put the single right solution on the blackboard, or should he

(2) put every possible wrong solution on the blackboard, and by process of
elimination arrive at the solution by identifying all the solutions that
are not correct?

Which approach requires more blackboard space, and more time?

-- 
Paul Lutus
http://www.arachnoid.com