changing the spacing in a string?

Discussion in 'Ruby' started by David Ainley, Aug 10, 2010.

  1. David Ainley

    David Ainley Guest

    Hey guys. I have an array full of strings. When I put the strings, it
    looks like this http://pastebin.com/eeicUCqL which is okay, but it looks
    a little sloppy to me. Is there a way I can get the strings to
    dynamically space themselves so that they look evenly spaced? ala
    http://pastebin.com/cRcG97sK ? Should I split each string into it's
    separate parts, and then print them via format? Or something...?
    --
    Posted via http://www.ruby-forum.com/.
    David Ainley, Aug 10, 2010
    #1
    1. Advertising

  2. On Tue, Aug 10, 2010 at 8:51 AM, David Ainley <> wrote:
    > Hey guys. =A0I have an array full of strings. =A0When I put the strings, =

    it
    > looks like this http://pastebin.com/eeicUCqL which is okay, but it looks
    > a little sloppy to me. =A0Is there a way I can get the strings to
    > dynamically space themselves so that they look evenly spaced? ala
    > http://pastebin.com/cRcG97sK ? =A0Should I split each string into it's
    > separate parts, and then print them via format? =A0Or something...?


    You've got a few options, depending on how sophisticated you want to
    get. Based on that small sample, it looks like the easiest thing to
    do might be to replace runs of multiple spaces with a single tab.
    Something like:

    str.gsub( /\s+/, "\t" )

    Otherwise, you could definitely split it on spaces and print using
    #printf and a carefully-crafted format string.

    Ben
    Ben Bleything, Aug 10, 2010
    #2
    1. Advertising

  3. David Ainley

    David Ainley Guest

    Ben Bleything wrote:
    > On Tue, Aug 10, 2010 at 8:51 AM, David Ainley <>
    > wrote:
    >> Hey guys. �I have an array full of strings. �When I put the strings, it
    >> looks like this http://pastebin.com/eeicUCqL which is okay, but it looks
    >> a little sloppy to me. �Is there a way I can get the strings to
    >> dynamically space themselves so that they look evenly spaced? ala
    >> http://pastebin.com/cRcG97sK ? �Should I split each string into it's
    >> separate parts, and then print them via format? �Or something...?

    >
    > You've got a few options, depending on how sophisticated you want to
    > get. Based on that small sample, it looks like the easiest thing to
    > do might be to replace runs of multiple spaces with a single tab.
    > Something like:
    >
    > str.gsub( /\s+/, "\t" )
    >
    > Otherwise, you could definitely split it on spaces and print using
    > #printf and a carefully-crafted format string.
    >
    > Ben


    Thanks for the response! It seems to be working, except for the two
    packages that deal with AlexanderHungenberg. With one tab, his name is
    too long for the date to be formatted correctly (
    http://pastebin.com/g8gEcvhJ ). I thought that having two tabs instead
    of one might fix the problemm but then the date is still jutted out (
    http://pastebin.com/CB13TZ6E ). This isn't that massive of a problem
    (Alexander just has a unconditionally long username), but, anyways, any
    ideas?
    --
    Posted via http://www.ruby-forum.com/.
    David Ainley, Aug 10, 2010
    #3
  4. David Ainley

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Tue, Aug 10, 2010 at 10:51 AM, David Ainley <> wrote:

    > Hey guys. I have an array full of strings. When I put the strings, it
    > looks like this http://pastebin.com/eeicUCqL which is okay, but it looks
    > a little sloppy to me. Is there a way I can get the strings to
    > dynamically space themselves so that they look evenly spaced? ala
    > http://pastebin.com/cRcG97sK ? Should I split each string into it's
    > separate parts, and then print them via format? Or something...?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    # counts the length of each string based on column
    # returns the max for each column across all rows
    def get_widths(array_of_arrays_of_strings)
    widths = Array.new
    array_of_arrays_of_strings.each do |strings|
    strings.each_with_index do |string,index|
    widths[index] = string.length if !widths[index] || widths[index] <
    string.length
    end
    end
    widths
    end


    # the data itself -- you haven't show what format it is in, this is just a
    guess
    data = [
    'gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)',
    'gm-notify 0.9-0~ppa6 TomVetterlein (2009-05-15)' ,
    'gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-13)',
    'gm-notify 0.9+r39-1~ppa1 SeanStoops (2009-12-22)' ,
    'gm-notify 0.9-0~ppa7 NikolaKovacs (2009-09-27)' ,
    'gm-notify 0.8-0ppa2 TomVetterlein (2009-04-27)' ,
    ]

    # split the strings on whitespace so they are now arrays of strings
    data.map!(&:split)

    # get an array of max widths for each row
    widths = get_widths data # => [9, 14, 19, 12]

    # turn the widths into a format string (contains the spacing information)
    format = widths.map { |width| "%-#{width}s" }.join(' ')
    format # => "%-9s %-14s %-19s %-12s"

    # format and output the strings
    data.each { |strings| puts format % strings }
    # >> gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)
    # >> gm-notify 0.9-0~ppa6 TomVetterlein (2009-05-15)
    # >> gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-13)
    # >> gm-notify 0.9+r39-1~ppa1 SeanStoops (2009-12-22)
    # >> gm-notify 0.9-0~ppa7 NikolaKovacs (2009-09-27)
    # >> gm-notify 0.8-0ppa2 TomVetterlein (2009-04-27)
    Josh Cheek, Aug 10, 2010
    #4
  5. David Ainley

    Guest

    On Tue, Aug 10, 2010 at 11:51 AM, David Ainley <> wrote:
    > Hey guys. =A0I have an array full of strings. =A0When I put the strings, =

    it
    > looks like this http://pastebin.com/eeicUCqL which is okay, but it looks
    > a little sloppy to me. =A0Is there a way I can get the strings to
    > dynamically space themselves so that they look evenly spaced? ala
    > http://pastebin.com/cRcG97sK ? =A0Should I split each string into it's
    > separate parts, and then print them via format? =A0Or something...?


    And for a different approach :)

    /tmp> cat i.rb
    str =3D Array.new
    str << "gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)"
    str << "gm-notify 0.9-0~ppa6 TomVetterlein (2009-05-15)"
    str << "gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-13)"
    str << "gm-notify 0.9+r39-1~ppa1 SeanStoops (2009-12-22)"
    str << "gm-notify 0.9-0~ppa7 NikolaKovacs (2009-09-27)"
    str << "gm-notify 0.8-0ppa2 TomVetterlein (2009-04-27)"

    x =3D IO.popen("column -t", "w+"){|p|
    p.puts str.join("\n")
    p.close_write
    p.read
    }

    puts x

    /tmp> ruby i.rb
    gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-05)
    gm-notify 0.9-0~ppa6 TomVetterlein (2009-05-15)
    gm-notify 0.10.2-1~ppa1 AlexanderHungenberg (2010-05-13)
    gm-notify 0.9+r39-1~ppa1 SeanStoops (2009-12-22)
    gm-notify 0.9-0~ppa7 NikolaKovacs (2009-09-27)
    gm-notify 0.8-0ppa2 TomVetterlein (2009-04-27)
    , Aug 10, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Craig Deelsnyder [MVP]

    Re: Spacing radio buttons in a PlaceHolder

    Craig Deelsnyder [MVP], Jul 15, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    419
    Craig Deelsnyder [MVP]
    Jul 15, 2003
  2. Stephan Bour

    Row spacing in DataList

    Stephan Bour, Dec 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    547
    Stephan Bour
    Dec 8, 2003
  3. Greg Linwood

    Spacing columns in DataGrid

    Greg Linwood, Dec 18, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    657
    Greg Linwood
    Dec 19, 2003
  4. KatB
    Replies:
    1
    Views:
    582
    Lucas Tam
    Oct 5, 2004
  5. KatB
    Replies:
    4
    Views:
    824
Loading...

Share This Page