#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    3
    Rep 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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]

    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
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    3
    Rep Power
    0
    Thanks A LOT. That will work just fine to get me started
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    ruby tutorials


    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]

    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/

IMN logo majestic logo threadwatch logo seochat tools logo