[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: format problem

Li Chen

12/10/2006 10:13:00 AM

original 1D array
[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]

I change 1D array into 2D array:
2D array in 8 columns x 12 rows or
2D array in 12 columns x8 rows.

If column number is <10 I get expected output
format(each line is a row containing 8 columns):
[[2294.4, 3481.2, 2716.7, 1672.2, 1135.3, 2103.5,
591.1, 648.5],

...
[340.2, 488.2,13.0, 14.1, 14.1, 16.2, 16.1, 27.1]

]

If column number is >=10 let say 12, I get unwanted
output(each line is a column):

[[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]]

Li



____________________________________________________________________________________
Yahoo! Music Unlimited
Access over 1 million songs.
http://music.yahoo.com...

16 Answers

Paul Lutus

12/10/2006 5:34:00 PM

0

chen li wrote:

> original 1D array

/ ... snip result listing

> Li

It would be very helpful is you were to post the code you used to get this
result.

Again, I am just guessing about what you want, but try this:

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

#!/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

p slices

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

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

Eric Hodel

12/10/2006 7:50:00 PM

0

On Dec 10, 2006, at 02:12 , chen li wrote:

> original 1D array
> [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]
>
> I change 1D array into 2D array:
> 2D array in 8 columns x 12 rows or
> 2D array in 12 columns x8 rows.
>
> If column number is <10 I get expected output
> format(each line is a row containing 8 columns):
> [[2294.4, 3481.2, 2716.7, 1672.2, 1135.3, 2103.5,
> 591.1, 648.5],
>
> ...
> [340.2, 488.2,13.0, 14.1, 14.1, 16.2, 16.1, 27.1]
>
> ]
>
> If column number is >=10 let say 12, I get unwanted
> output(each line is a column):
>
> [[2294.4,
> 3481.2,
> 2716.7,
> 1672.2,
> 1135.3,
> 2103.5,
> 591.1,
> 648.5,
> 603.0,
> 477.2,
> 264.1,
> 626.5],

require 'pp'

PP.pp arr, 100 # you'll have to fudge this number

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Li Chen

12/11/2006 4:28:00 AM

0

Paul Lutus wrote:
>
> It would be very helpful is you were to post the code you used to get
> this
> result.
>

My script is about 140 lines. I am not sure if I post it.

Li

--
Posted via http://www.ruby-....

Paul Lutus

12/11/2006 4:39:00 AM

0

Li Chen wrote:

> Paul Lutus wrote:
>>
>> It would be very helpful is you were to post the code you used to get
>> this
>> result.
>>
>
> My script is about 140 lines. I am not sure if I post it.

Did my previously posted code example solve your problem? If you will be
specific enough about the problem and its roots in your Ruby code, we might
be able to resolve the issue without examining all your source.

You should be able to create an abbreviated version of your program that
simply creates and displays the problem array. That would allow an easy
analysis and fix.

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

Li Chen

12/11/2006 4:50:00 AM

0

Paul Lutus wrote:

> Did my previously posted code example solve your problem? If you will be
> specific enough about the problem and its roots in your Ruby code, we
> might
> be able to resolve the issue without examining all your source.


I need sometime to think about your codes. Although my codes have
problem on printing the expected format it can correctly import the data
into Excel.

Thanks,

Li


--
Posted via http://www.ruby-....

Li Chen

12/11/2006 5:18:00 AM

0

Paul Lutus wrote:

> You should be able to create an abbreviated version of your program that
> simply creates and displays the problem array. That would allow an easy
> analysis and fix.

Hi Paul,

I run your codes and it works fine.

And here is my codes with the problem:

###
require 'pp'
require 'enumerator'


file_name='C:\Ruby\self\exp\BMDC5-3H\EXPT.083'

#read the data file
data =[]
_1D_array=[]

f=File.open(file_name)
f.each do |line|
next if line=~/CPM/
# get the 3th column for each line
_1D_array<<line.chomp.split()[2].to_f
end

# transform the 1D array into 2D array
columns=12
_1D_array.each_slice(columns) do|slice|
data<<slice
end

pp data

###output
[[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


--
Posted via http://www.ruby-....

Paul Lutus

12/11/2006 6:20:00 AM

0

Li Chen wrote:

> Paul Lutus wrote:
>
>> You should be able to create an abbreviated version of your program that
>> simply creates and displays the problem array. That would allow an easy
>> analysis and fix.
>
> Hi Paul,
>
> I run your codes and it works fine.

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?

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

Li Chen

12/11/2006 12:51:00 PM

0

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. If I just use p I get all the elements on
one line. But I still can see the result is a 2D array even they are
printed one line. For me I need the format in result A only.

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]]
>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.

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

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.

Again thank you very much for your time,

Li







--
Posted via http://www.ruby-....

Paul Lutus

12/11/2006 5:30:00 PM

0

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.ara...

dblack

12/11/2006 6:45:00 PM

0