change in number of leading spaces

Discussion in 'Ruby' started by Jesse B., Apr 1, 2010.

  1. Jesse B.

    Jesse B. Guest

    Sorry about re-post due to typo.

    I am looping through a file and using a script that detects leading
    spaces (based on a previous post to this forum)

    What I want to do is to detect changes in number of leading spaces, for
    instance when one line has more, fewer, or the same number of leading
    spaces as the previous line.

    The problem I am having is with setting a value from within the loop
    which is then available on the next loop.

    here is an example of the program:

    infile = File.new("input.txt", "r")
    outfile = File.new("output.txt", "w")
    infile.each_line {
    |i|

    spaces = i[/\A */].length

    #the following variable "stored" puts undef every time through, which
    means #it's not retaining the value on the next loop?

    if defined?(stored)== true
    puts stored

    elsif defined?(stored)== nil
    puts "undef"
    stored = spaces
    end


    case spaces
    when 1
    output= "onespace"

    when 2
    output="2 spaces"

    end


    outfile.write output
    }

    outfile.close()
    infile.close()


    outfile = File.new("output.txt", "r")
    outfile.each_line {
    |i|
    print ">>", i
    }
    --
    Posted via http://www.ruby-forum.com/.
     
    Jesse B., Apr 1, 2010
    #1
    1. Advertising

  2. 2010/4/1 Jesse B. <>:
    > Sorry about re-post due to typo.
    >
    > I am looping through a file and using a script that detects leading
    > spaces (based on a previous post to this forum)
    >
    > What I want to do is to detect changes in number of leading spaces, for
    > instance when one line has more, fewer, or the same number of leading
    > spaces as the previous line.
    >
    > The problem I am having is with setting a value from within the loop
    > which is then available on the next loop.
    >
    > here is an example of the program:
    >
    > infile =3D File.new("input.txt", "r")


    You can simplify this by using

    File.foreach "input.txt" do |line|
    ...
    end

    > outfile =3D File.new("output.txt", "w")


    Also, please make it a habit to use the block form of File.open which
    is much safer. So this becomes

    File.open("output.txt", "w") do |outfile|
    ...
    end

    For some background:
    http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robust=
    ness.html

    > infile.each_line {
    > =A0|i|
    >
    > spaces =3D i[/\A */].length
    >
    > #the following variable "stored" puts undef every time through, which
    > means #it's not retaining the value on the next loop?
    >
    > if defined?(stored)=3D=3D true
    > puts stored


    As Alex already pointed out this should be defined outside the loop.

    Here's another variant

    def count_leading_spaces(s)
    s[/\A */].length # or any other method that works
    end

    sp =3D 0

    ARGF.each_line do |line|
    i =3D count_leading_spaces line

    if i !=3D sp
    line.chomp! # avoid duplicate \n
    printf "Change from %2d to %2d spaces in this line %4d: %s\n",
    sp, i, ARGF.lineno, line
    sp =3D i
    end
    end


    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Apr 1, 2010
    #2
    1. Advertising

  3. Jesse B.

    Jesse B. Guest

    Robert, Thank you for taking the time to write back.
    I got the initial file operations you gave me working like so:

    File.open("output.txt", "w") do |outfile|
    File.foreach "input.txt" do |line|
    puts line
    outfile.write line
    end
    end

    which both displays the results on the command line and in the
    output.txt file

    then I tried to add in the rest like so

    File.open("output.txt", "w") do |outfile|
    File.foreach "input.txt" do |line|

    def count_leading_spaces(s)
    s[/\A */].length # or any other method that works
    end

    sp = 0

    ARGF.each_line do |line|
    i = count_leading_spaces line

    if i != sp
    line.chomp! # avoid duplicate \n
    printf "Change from %2d to %2d spaces in this line %4d: %s\n",
    sp, i, ARGF.lineno, line
    sp = i
    end
    end


    #puts line
    #outfile.write line
    end
    end

    and when I save as a ruby file and run from the command line nothing
    happens, until i hit control-c and get a list of errors.

    I think the problem is that I am incorrectly assembling your snippets.

    Would it be possible to redo this as a complete example that works as a
    ruby file run from the command line?

    thank you in advance

    --
    Posted via http://www.ruby-forum.com/.
     
    Jesse B., Apr 4, 2010
    #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. John Nagle
    Replies:
    4
    Views:
    599
    James Antill
    Aug 7, 2007
  2. Delaney, Timothy (Tim)
    Replies:
    0
    Views:
    571
    Delaney, Timothy (Tim)
    Aug 7, 2007
  3. John B. Matthews
    Replies:
    4
    Views:
    675
    John B. Matthews
    Sep 12, 2008
  4. Roedy Green
    Replies:
    3
    Views:
    630
  5. Jesse B.
    Replies:
    0
    Views:
    86
    Jesse B.
    Mar 31, 2010
Loading...

Share This Page