Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 21st, 2011, 10:12 AM
radon7 radon7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 3 radon7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 36 sec
Reputation Power: 0
Question Formatting stdout output

I am new to Ruby and ran into a small problem. Everything I find on formatting is for strings or such. I am trying to output a hash as a table to the shell. I'm thinking like 5 key and value pairs per line. I would prefer it to increase going down rather than left to right but either would work. The best example I have is the way 'ls' on linux will list the files in a dir. That is an array rather than a hash though.

Here is the function and x is the hash on input

def output(x)
x.sort.map.each do |key, val|
print ' ___ '+key.to_s+' = '+val.to_s
end
puts
end

This displays one long row rather than splitting it up. And putting 'puts' instead makes 1 long column.

I appreciate any help I can get. Every thing I have tried has been helpless.

Radon

Reply With Quote
  #2  
Old February 21st, 2011, 11:32 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
Well, its going to be difficult unless all of your data is the same exact size. Do you expect all of the '=' characters to line up nicely in your output? Do you need the exact number of columns on each line (like ls will do for you)?
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

Reply With Quote
  #3  
Old February 21st, 2011, 12:14 PM
radon7 radon7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 3 radon7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 36 sec
Reputation Power: 0
Quote:
Originally Posted by L7Sqr
Well, its going to be difficult unless all of your data is the same exact size. Do you expect all of the '=' characters to line up nicely in your output? Do you need the exact number of columns on each line (like ls will do for you)?



It would be nice if all the '=' lined up nicely but it wouldn't have to. I just mainly don't want the (key, val) pair split at the end of the shell line or one real long column. I am runnin some hashes through the function that has only like 6 pairs but others have a hundred or so.

This is what i have in mind
a=1 e=4 h=7 k=10
b=2 f=5 i=8
c=3 g=6 j=9

or if need be
a=1 b=2 c=3 d=4
e=5 f=6 g=7 h=8
i=9 j=10

I know is some languages you can make a table from an array but I don't know about a hash. I know you can flatten it down into an array. What I was thinking was like flattening the hash to an array then combine every two into a string like 'a=1' by joining with a '=' then putting it into another array of the strings so you could just iterate over it every nth time and printing n strings on each line.

I am to the point though anything that will be readable would be ok for now.

Thanks for any help
Radon

Reply With Quote
  #4  
Old February 21st, 2011, 07:44 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
The following code will accept the hash and format it into strings of key/value pairs (joined with '=') and print out to a particular width. The width will be exceeded it the key/value pair is greater than the overall width and the the '=' are not aligned. You can work that in if you'd like. Below are sample runs.

Code:
ruby
def format(hsh,width=80)
    used = 0
    hsh.collect { |k,v| [k,v].join "=" }.each do |str|
        if used > 0 and used + str.size >= width
            puts
            used = 0
        end
        print "#{str} "
        used += str.size + 1
    end
    puts
end


Code:
# format h
yetmore=yousay somekey=somevalue 1=2 3=45500034 anotherkey=anothervalue 
332435=1123243 

Code:
 # format h, 20
yetmore=yousay 
somekey=somevalue 
1=2 3=45500034 
anotherkey=anothervalue 
332435=1123243

Reply With Quote
  #5  
Old February 23rd, 2011, 11:22 AM
radon7 radon7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 3 radon7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 54 m 36 sec
Reputation Power: 0
Thanks A LOT. That will work just fine to get me started

Reply With Quote
  #6  
Old June 11th, 2011, 01:16 AM
azhagu azhagu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 1 azhagu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 3 sec
Reputation Power: 0
ruby tutorials

Quote:
Originally Posted by L7Sqr
The following code will accept the hash and format it into strings of key/value pairs (joined with '=') and print out to a particular width. The width will be exceeded it the key/value pair is greater than the overall width and the the '=' are not aligned. You can work that in if you'd like. Below are sample runs.

Code:
ruby
def format(hsh,width=80)
    used = 0
    hsh.collect { |k,v| [k,v].join "=" }.each do |str|
        if used > 0 and used + str.size >= width
            puts
            used = 0
        end
        print "#{str} "
        used += str.size + 1
    end
    puts
end


Code:
# format h
yetmore=yousay somekey=somevalue 1=2 3=45500034 anotherkey=anothervalue 
332435=1123243 

Code:
 # format h, 20
yetmore=yousay 
somekey=somevalue 
1=2 3=45500034 
anotherkey=anothervalue 
332435=1123243


See this Ruby Tutorials
http://techpdf.co.cc/blog/rubby/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Formatting stdout output

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap