[File -> Lines -> Chars] Logic issue (open)

Discussion in 'Ruby' started by Flaab Mrlinux, Nov 15, 2006.

  1. Hi there

    Just a simple question from a newbie =)

    I have a file that can have N lines. Those lines are string of
    characters, and each character is a sign of a combination i have to
    store, but at the same time i have to be able to treat each sign of each
    combination separately, in order to compare them and all.

    For instance:

    # Combination file

    11XX22X1XX12X23
    XX12XX122X212X2
    XX2X11XX11XX111

    My program has to return and array for each line, and that array should
    have stored in it each sign of the line.

    My question is...how can i Read EACH line of the file, explode each char
    of the array and store it?

    Thx.

    --
    Posted via http://www.ruby-forum.com/.
    Flaab Mrlinux, Nov 15, 2006
    #1
    1. Advertising

  2. Flaab Mrlinux wrote:
    > Hi there
    >
    > Just a simple question from a newbie =)
    >
    > I have a file that can have N lines. Those lines are string of
    > characters, and each character is a sign of a combination i have to
    > store, but at the same time i have to be able to treat each sign of each
    > combination separately, in order to compare them and all.
    >
    > For instance:
    >
    > # Combination file
    >
    > 11XX22X1XX12X23
    > XX12XX122X212X2
    > XX2X11XX11XX111
    >
    > My program has to return and array for each line, and that array should
    > have stored in it each sign of the line.
    >
    > My question is...how can i Read EACH line of the file, explode each char
    > of the array and store it?
    >
    > Thx.
    >


    ary = File.open(file).readlines.map {|l|
    l.chomp.split //
    }

    --
    Vincent Fourmond, PhD student
    http://vincent.fourmond.neuf.fr/
    Vincent Fourmond, Nov 15, 2006
    #2
    1. Advertising

  3. On 15/11/06, Flaab Mrlinux <> wrote:
    > Hi there
    >
    > Just a simple question from a newbie =)
    >
    > I have a file that can have N lines. Those lines are string of
    > characters, and each character is a sign of a combination i have to
    > store, but at the same time i have to be able to treat each sign of each
    > combination separately, in order to compare them and all.
    >
    > For instance:
    >
    > # Combination file
    >
    > 11XX22X1XX12X23
    > XX12XX122X212X2
    > XX2X11XX11XX111
    >
    > My program has to return and array for each line, and that array should
    > have stored in it each sign of the line.
    >
    > My question is...how can i Read EACH line of the file, explode each char
    > of the array and store it?
    >
    > Thx.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    You can use each_byte to iterate over every character of a string.
    Just be aware that it will pass an int character code, not the actual
    character itself, into the associated block but you can convert it
    back using the chr method like so:
    irb(main):026:0> "hello".each_byte{|c|puts c.chr}
    h
    e
    l
    l
    o

    Farrel
    Farrel Lifson, Nov 15, 2006
    #3
    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. Joe Wright
    Replies:
    0
    Views:
    508
    Joe Wright
    Jul 27, 2003
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,285
    Tim Rentsch
    Sep 23, 2005
  3. Hongyu
    Replies:
    9
    Views:
    902
    James Kanze
    Aug 8, 2008
  4. spike
    Replies:
    8
    Views:
    1,463
    Steve Holden
    Feb 9, 2010
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    225
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page