newbie qusetion: read a file and print out same file

Discussion in 'Ruby' started by joemacbusiness@gmail.com, Dec 24, 2008.

  1. Guest

    Hi All,

    How do I read an input file and print it back to STDOUT
    exactly as the original file. This should be very simple
    but the output of my program is all garbled.
    What am I doing wrong?
    ( I am using http://www.rubycentral.com/book to learn Ruby)

    The program, input, and runtime output are below.

    Thanks, --JM
    ================= snip ==============

    $ cat ./test56.rb
    #!/usr/local/bin/ruby

    inputFile = File.readlines("input4", 'r')

    for item in inputFile
    item.chop()
    print item, "\n"
    end
    $
    $ cat input4
    car
    tree
    house
    candy bar
    paper
    ocean
    $ ./test56.rb
    car

    tr
    ee
    house
    candy bar

    paper

    ocean
    , Dec 24, 2008
    #1
    1. Advertising

  2. Hi --

    On Thu, 25 Dec 2008, wrote:

    > Hi All,
    >
    > How do I read an input file and print it back to STDOUT
    > exactly as the original file. This should be very simple
    > but the output of my program is all garbled.
    > What am I doing wrong?
    > ( I am using http://www.rubycentral.com/book to learn Ruby)
    >
    > The program, input, and runtime output are below.
    >
    > Thanks, --JM
    > ================= snip ==============
    >
    > $ cat ./test56.rb
    > #!/usr/local/bin/ruby
    >
    > inputFile = File.readlines("input4", 'r')
    >
    > for item in inputFile
    > item.chop()
    > print item, "\n"
    > end


    You're working too hard :)

    input_file = File.readlines("input4")
    puts input_file

    or just

    puts File.read("input4")

    If you do have occasion to loop through an array of lines (which no
    doubt you will at some point), remember that chop doesn't do a
    permanent chop. What you probably want is chomp!, which removes a
    trailing newline if there is one.


    David

    --
    David A. Black / Ruby Power and Light, LLC
    Ruby/Rails consulting & training: http://www.rubypal.com
    Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)
    David A. Black, Dec 24, 2008
    #2
    1. Advertising

  3. Guest

    On Dec 24, 9:26 am, "David A. Black" <> wrote:
    > Hi --
    >
    >
    >
    > On Thu, 25 Dec 2008, wrote:
    > > Hi All,

    >
    > > How do I read an input file and print it back to STDOUT
    > > exactly as the original file. This should be very simple
    > > but the output of my program is all garbled.
    > > What am I doing wrong?
    > > ( I am usinghttp://www.rubycentral.com/bookto learn Ruby)

    >
    > > The program, input, and runtime output are below.

    >
    > > Thanks, --JM
    > > ================= snip ==============

    >
    > > $ cat ./test56.rb
    > > #!/usr/local/bin/ruby

    >
    > > inputFile = File.readlines("input4", 'r')

    >
    > > for item in inputFile
    > > item.chop()
    > > print item, "\n"
    > > end

    >
    > You're working too hard :)
    >
    > input_file = File.readlines("input4")
    > puts input_file
    >
    > or just
    >
    > puts File.read("input4")
    >
    > If you do have occasion to loop through an array of lines (which no
    > doubt you will at some point), remember that chop doesn't do a
    > permanent chop. What you probably want is chomp!, which removes a
    > trailing newline if there is one.
    >
    > David
    >
    > --
    > David A. Black / Ruby Power and Light, LLC
    > Ruby/Rails consulting & training:http://www.rubypal.com
    > Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)



    Hello David,

    I wanted to loop through the input file "line-by-line"
    and print it out the same way. Sorry I did not make that
    clear.
    Here is the code block that does what I wanted.

    inputFile = File.open("input4", 'r')
    while line = inputFile.gets()
    puts line
    end

    Thank you, --JM
    , Dec 24, 2008
    #3
  4. unknown wrote:
    > $ cat input4
    > car
    > tree
    > house
    > candy bar
    > paper
    > ocean
    > $ ./test56.rb
    > car
    >
    > tr
    > ee
    > house
    > candy bar
    >
    > paper
    >
    > ocean


    Interesting output, especially the split 'tree'. To see what's going on,
    I added an extra line to your code:

    inputFile = File.readlines("input4", 'r')
    p inputFile

    This gives the following output:

    ["car", "\ntr", "ee\nhouse\ncandy bar", "\npaper", "\nocean\n"]

    To understand what's happening here, read the documentation under "ri
    IO::readlines"

    ---------------------------------------------------------- IO::readlines
    IO.readlines(name, sep_string=$/) => array
    ------------------------------------------------------------------------
    Reads the entire file specified by _name_ as individual lines, and
    returns those lines in an array. Lines are separated by
    _sep_string_.

    a = IO.readlines("testfile")
    a[0] #=> "This is line one\n"

    So, the problem is that the 'r' is not the file input mode, but the
    separator character! The fact you had the words "car", "bar" and "paper"
    (all ending in 'r') made this rather unclear. It looks like it's mostly
    splitting at the end of a line, but actually it's splitting after a
    letter 'r'.

    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Dec 25, 2008
    #4
    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. =?Utf-8?B?UnVkeQ==?=

    Quick Qusetion on Sessions

    =?Utf-8?B?UnVkeQ==?=, Jun 22, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    339
    =?Utf-8?B?UnVkeQ==?=
    Jun 22, 2005
  2. keto
    Replies:
    0
    Views:
    896
  3. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    334
    David Cournapeau
    Dec 30, 2008
  4. Jack

    Select's Qusetion

    Jack, Jan 19, 2004, in forum: ASP General
    Replies:
    6
    Views:
    102
  5. Marek Stepanek

    Beginner: read and print same file

    Marek Stepanek, Aug 13, 2006, in forum: Perl Misc
    Replies:
    6
    Views:
    104
    John W. Krahn
    Aug 14, 2006
Loading...

Share This Page