Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > format problem

Reply
Thread Tools

format problem

 
 
chen li
Guest
Posts: n/a
 
      12-10-2006
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.yahoo.com

 
Reply With Quote
 
 
 
 
Li Chen
Guest
Posts: n/a
 
      12-11-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Li Chen
Guest
Posts: n/a
 
      12-11-2006
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-forum.com/.

 
Reply With Quote
 
Li Chen
Guest
Posts: n/a
 
      12-11-2006
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-forum.com/.

 
Reply With Quote
 
Li Chen
Guest
Posts: n/a
 
      12-11-2006
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 pall 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-forum.com/.

 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      12-11-2006
Hi --

On Mon, 11 Dec 2006, Paul Lutus wrote:

> What is 'pp'?


The pretty-printing library in the standard Ruby distribution. See
pp.rb.


David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Li Chen
Guest
Posts: n/a
 
      12-11-2006
Paul Lutus wrote:
> This principle is especially potent with Ruby, where it is very easy to
> solve problems by writing a few lines of code, code you can still
> understand months or years later.
>
> Not to pontificate here, but ... when a library -- the "easy way" --
> takes
> more time to use than hand coding -- the "hard way" -- the library loses
> its right to exist.
>
> These are just my opinions, reasonable people can and will differ.


I completely agree with you. And thank you all for time and your reply.

Li


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Money Format + Decimal Place Format shannon Java 1 02-02-2006 03:47 PM
Money Format + Decimal Place Format shannon Java 0 02-01-2006 10:02 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
NTFS quick format and normal format Guan Foo Wah MCSE 2 05-08-2004 11:35 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM



Advertisments