Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > formatting a listing

Reply
Thread Tools

formatting a listing

 
 
George George
Guest
Posts: n/a
 
      06-15-2009
i have a listing which looks like this
1
2
3

3
4
5

20
3
5
5

i would like a quick way to format that output such that i end up with 3
columns like the following.
1,2,20
2,4,3
3,5,5
5

any ideas?
thanks
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      06-15-2009
On Mon, Jun 15, 2009 at 12:46 PM, George
George<(E-Mail Removed)> wrote:
> i have a listing which looks like this
> 1
> 2
> 3
>
> 3
> 4
> 5
>
> 20
> 3
> 5
> 5
>
> i would like a quick way to format that output such that i end up with 3
> columns like the following.
> 1,2,20
> 2,4,3
> 3,5,5
> =A0 =A05
>
> any ideas?
> thanks


If this is a homework assignement I am terribly sorry, but I cannot know.

puts DATA.inject( [ [] ] ){ | ary, ele |
ele.strip.empty? ? ary << [] : ary.last << ele.to_i # or ele.chomp
if you prefer
ary
}
reject( &:empty? ) # .map( &:join, ", " ) is not standard Ruby
map{ | eles | eles.join( ", " )}

__END__
1
2

42
44
46

2000

HTH
Robert
--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]

 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-15-2009
George George <(E-Mail Removed)> writes:

> i have a listing which looks like this
> 1
> 2
> 3
>
> 3
> 4
> 5
>
> 20
> 3
> 5
> 5
>
> i would like a quick way to format that output such that i end up with 3
> columns like the following.
> 1,2,20
> 2,4,3
> 3,5,5
> 5
>
> any ideas?


It's hard to tell, since there seem to be random changes in the output
data vs. the input. Well, one random change. Or do you really mean
to subtract 1 from the first row, second column of the output?
What about the missing commas? Why the numbers are left-aligned?




(printf "%s\n\n" ,
(begin
(matrix = (Array . new))
(ncols = 1)
(matrix . push(Array . new))
(((IO . readlines'/tmp/test.data') .
map { | line | (line . strip) }) .
each { | item | (if (item == "")
(ncols = (ncols + 1))
(matrix . push(Array . new))
else
((matrix . at(ncols - 1)) . push item)
end)
})
(height = ((matrix . map { | column | (column . size) }) . max))
(widths = (matrix . map { | column | ((column . map { | cell | ((sprintf("%s" , cell)) . size) }) . max) }))
((((matrix . map { | column | (column + (Array . new((height - (column . size)) , ""))) }) .
transpose) .
map { | row | ((([ row , widths ] . transpose) .
map { | r , w | (sprintf( "%*s" , w , r)) }) .
join ",")}) .
join "\n")
end))

1,3,20
2,4, 3
3,5, 5
, , 5


--
__Pascal Bourguignon__
 
Reply With Quote
 
George George
Guest
Posts: n/a
 
      06-15-2009
Robert Dober wrote:
> On Mon, Jun 15, 2009 at 12:46 PM, George
> George<(E-Mail Removed)> wrote:
>> 3
>> any ideas?
>> thanks

>
> If this is a homework assignement I am terribly sorry, but I cannot
> know.
>
> puts DATA.inject( [ [] ] ){ | ary, ele |
> ele.strip.empty? ? ary << [] : ary.last << ele.to_i # or ele.chomp
> if you prefer
> ary
> }
> .reject( &:empty? ) # .map( &:join, ", " ) is not standard Ruby
> .map{ | eles | eles.join( ", " )}
>
> __END__
> 1
> 2
>
> 42
> 44
> 46
>
> 2000
>
> HTH
> Robert


Thank so much for pointing the way: And this is not assignment! Am
working on some huge datasets that run in to up to 20 000 columns and
was looking for an easy way of creating the columns.


puts DATA.inject( [ [] ] ){ | ary, ele |
ele.strip.empty? ? ary << [] : ary.last << ele.to_i # or ele.chomp
#if you prefer
# ary
}
#.reject( &:empty? ) # .map( &:join, ", " ) is not standard Ruby
#.map{ | eles | eles.join( ", " )}

__END__
1
2

42
44
46

am not trying to ask too much but your code is real witchcraft
raises an error
`<<': can't convert Array into Integer (TypeError)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      06-15-2009
On Mon, Jun 15, 2009 at 2:12 PM, George George<(E-Mail Removed)> w=
rote:
> Robert Dober wrote:
>> On Mon, Jun 15, 2009 at 12:46 PM, George
>> George<(E-Mail Removed)> wrote:
>>> 3
>>> any ideas?
>>> thanks

>>
>> If this is a homework assignement I am terribly sorry, but I cannot
>> know.
>>
>> puts DATA.inject( [ =A0[] ] ){ | ary, ele |
>> =A0 ele.strip.empty? ? ary << [] : ary.last << ele.to_i # or ele.chomp
>> if you prefer
>> =A0 ary
>> }
>> .reject( &:empty? ) =A0# .map( &:join, ", " ) is not standard Ruby
>> .map{ | eles | eles.join( ", " )}
>>
>> __END__
>> 1
>> 2
>>
>> 42
>> 44
>> 46
>>
>> 2000
>>
>> HTH
>> Robert

>
> Thank so much for pointing the way: And this is not assignment! Am
> working on some huge datasets that run in to up to 20 000 columns and
> was looking for an easy way of creating the columns.

If you have perf problems do not worry, inject is slow replace
inject(x){ |a,e|
}
with
loc =3D x
each { |e|
loc << e # e.g
}
loc
# you will have a speedup of 2~3

 
Reply With Quote
 
George George
Guest
Posts: n/a
 
      06-15-2009
Pascal J. Bourguignon wrote:
> George George <(E-Mail Removed)> writes:
>
>> 3
>> any ideas?

> It's hard to tell, since there seem to be random changes in the output
> data vs. the input. Well, one random change. Or do you really mean
> to subtract 1 from the first row, second column of the output?
> What about the missing commas? Why the numbers are left-aligned?
>

thank you so much for the reply.
The first column is a column of numbers separated by some white space.
The idea is to capture the first block and create a column and the
capture the next one and create another column adjacent to the previous
one such that
if
1
2
3
whitespace(one or more)
3
4
5
6
whitespace(one or more)
20
2
 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      06-15-2009
On Mon, Jun 15, 2009 at 2:10 PM, Pascal J.
Bourguignon<(E-Mail Removed)> wrote:
> --
> __Pascal Bourguignon__

Really Pascal, you do *not* need to sign your mails

>




--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]

 
Reply With Quote
 
George George
Guest
Posts: n/a
 
      06-15-2009
.....
such that every block of numbers separated by white space becomes a new
row in the output e.g for he above case ....

what i meant was column not row(sorry)
what i need is to transform that into (call it a matrix)
such that every block of numbers separated by white space becomes a new
column in the output e.g for he above case

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

 
Reply With Quote
 
George George
Guest
Posts: n/a
 
      06-15-2009
(printf "%s\n\n" ,
(begin
(matrix = (Array.new))
(ncols = 1)
(matrix.push(Array.new))
(((IO.readlines'/home/george/test.data').
map { | line | (line.strip) }).
each { | item | (if (item == "")
(ncols = (ncols + 1))
(matrix.push(Array.new))
else
((matrix.at(ncols - 1)).push item)
end)
})
(height = ((matrix.map { | column | (column.size) }).max))
(widths = (matrix.map { | column | ((column.map { | cell |
((sprintf("%s" , cell)).size) }).max) }))
((((matrix.map { | column | (column + (Array.new((height -
(column.size)) , ""))) }) .
transpose).
map { | row | ((([ row , widths ].transpose).
map { | r , w | (sprintf( "%*s" , w , r)) }) .
join ",")}).
join "\n")
end))

produces an error:
in `sprintf': no implicit conversion from nil to integer (TypeError)

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

 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-15-2009
George George <(E-Mail Removed)> writes:

> (printf "%s\n\n" ,
> (begin
> (matrix = (Array.new))
> (ncols = 1)
> (matrix.push(Array.new))
> (((IO.readlines'/home/george/test.data').
> map { | line | (line.strip) }).
> each { | item | (if (item == "")
> (ncols = (ncols + 1))
> (matrix.push(Array.new))
> else
> ((matrix.at(ncols - 1)).push item)
> end)
> })
> (height = ((matrix.map { | column | (column.size) }).max))
> (widths = (matrix.map { | column | ((column.map { | cell |
> ((sprintf("%s" , cell)).size) }).max) }))
> ((((matrix.map { | column | (column + (Array.new((height -
> (column.size)) , ""))) }) .
> transpose).
> map { | row | ((([ row , widths ].transpose).
> map { | r , w | (sprintf( "%*s" , w , r)) }) .
> join ",")}).
> join "\n")
> end))
>
> produces an error:
> in `sprintf': no implicit conversion from nil to integer (TypeError)



Well, I tried it with this file:
-----
1
2
3

3
4
5

20
3
5
5

-----

You may try to execute it expression per expression, and see where that nil comes from?


irb(main):850:0>
(matrix = (Array . new))
[]
irb(main):856:0>
(ncols = 1)
1
irb(main):862:0>
(matrix . push(Array . new))
[[]]
irb(main):868:0>
(((IO . readlines'/tmp/test.data') .
map { | line | (line . strip) }) .
each { | item | (if (item == "")
(ncols = (ncols + 1))
(matrix . push(Array . new))
else
((matrix . at(ncols - 1)) . push item)
end)
})
["1", "2", "3", "", "3", "4", "5", "", "20", "3", "5", "5"]
irb(main):890:0>
(height = ((matrix . map { | column | (column . size) }) . max))
4
irb(main):896:0>
(widths = (matrix . map { | column | ((column . map { | cell | ((sprintf("%s" , cell)) . size) }) . max) }))
[1, 1, 2]
irb(main):902:0>
((((matrix . map { | column | (column + (Array . new((height - (column . size)) , ""))) }) .
transpose) .
map { | row | ((([ row , widths ] . transpose) .
map { | r , w | (sprintf( "%*s" , w , r)) }) .
join ",")}) .
join "\n")
"1,3,20\n2,4, 3\n3,5, 5\n , , 5"
irb(main):918:0>
(begin
(matrix = (Array . new))
(ncols = 1)
(matrix . push(Array . new))
(((IO . readlines'/tmp/test.data') .
map { | line | (line . strip) }) .
each { | item | (if (item == "")
(ncols = (ncols + 1))
(matrix . push(Array . new))
else
((matrix . at(ncols - 1)) . push item)
end)
})
(height = ((matrix . map { | column | (column . size) }) . max))
(widths = (matrix . map { | column | ((column . map { | cell | ((sprintf("%s" , cell)) . size) }) . max) }))
matrix
((((matrix . map { | column | (column + (Array . new((height - (column . size)) , ""))) }) .
transpose) .
map { | row | ((([ row , widths ] . transpose) .
map { | r , w | (sprintf( "%*s" , w , r)) }) .
join ",")}) .
join "\n")
end)
"1,3,20\n2,4, 3\n3,5, 5\n , , 5"
irb(main):968:0>
(matrix = (Array . new))
[]
irb(main):974:0>
(ncols = 1)
1
irb(main):980:0>
(matrix . push(Array . new))
[[]]
irb(main):986:0>
(((IO . readlines'/tmp/test.data') .
map { | line | (line . strip) }) .
each { | item | (if (item == "")
(ncols = (ncols + 1))
(matrix . push(Array . new))
else
((matrix . at(ncols - 1)) . push item)
end)
})
["1", "2", "3", "", "3", "4", "5", "", "20", "3", "5", "5"]
irb(main):1005:0>
matrix
[["1", "2", "3"], ["3", "4", "5"], ["20", "3", "5", "5"]]
irb(main):1008:0>
(height = ((matrix . map { | column | (column . size) }) . max))
4
irb(main):1014:0>
(widths = (matrix . map { | column | ((column . map { | cell | ((sprintf("%s" , cell)) . size) }) . max) }))
[1, 1, 2]
irb(main):1020:0>
matrix
[["1", "2", "3"], ["3", "4", "5"], ["20", "3", "5", "5"]]
irb(main):1026:0>
((((matrix . map { | column | (column + (Array . new((height - (column . size)) , ""))) }) .
transpose) .
map { | row | ((([ row , widths ] . transpose) .
map { | r , w | (sprintf( "%*s" , w , r)) }) .
join ",")}) .
join "\n")
"1,3,20\n2,4, 3\n3,5, 5\n , , 5"
irb(main):1042:0>

--
__Pascal Bourguignon__
 
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
Listing installed perl modules gusmeister Perl 3 03-03-2004 08:57 PM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM
Listing MAC Table with SNMP Michel Lapointe Cisco 2 11-07-2003 01:40 PM
Directory Listing JD Perl 0 08-23-2003 11:53 PM
Listing processes using Perl (Win32) Filipe Bonjour Perl 0 07-26-2003 04:49 PM



Advertisments