array.each puzzle from new ruby-er

Discussion in 'Ruby' started by Gani Ruthellen, Aug 7, 2007.

  1. Given:
    lines = file.readlines("\x15") # don't ask :)
    lines.each do |y|
    y.chomp!("\x15")
    y.sub!(/^\n/,"") #kill off the extra line feeds
    if y =~ /^\*\*\*/ then
    y = "<someTag>" + y + "</someTag>"
    ...
    [ assume ends]
    puts lines

    my output for the affected elements is:
    <someTag>
    value of y</someTag>

    How can I get the results to be
    <someTag>value of y</someTag>

    I've tried changing y to y.to_s; I still get the new line.

    I'm sure this is obvious if you know what you're doing, but I'm stumped.
    Can somebody give me a clue?

    TIA if you take a moment to point me in the right direction.
    Gani
    --
    Posted via http://www.ruby-forum.com/.
     
    Gani Ruthellen, Aug 7, 2007
    #1
    1. Advertising

  2. Gani Ruthellen

    Dan Zwell Guest

    Gani Ruthellen wrote:
    > Given:
    > lines = file.readlines("\x15") # don't ask :)
    > lines.each do |y|
    > y.chomp!("\x15")
    > y.sub!(/^\n/,"") #kill off the extra line feeds

    I would think it better to just call y.chomp! here (with no argument).

    > if y =~ /^\*\*\*/ then
    > y = "<someTag>" + y + "</someTag>"

    Know that this is actually rebinding the local variable y to point to a
    new object, not modifying the old object. If your goal was to modify the
    array "lines", you should change "lines.each" to "lines.map" and make
    sure you end the block with a statement that returns the modified y.

    perhaps:
    lines.map do |y|
    y = y.chomp("\x15").chomp
    y = "<someTag>" + y + "</someTag>" if y =~ /^\*\*\*/
    y
    end


    Dan
     
    Dan Zwell, Aug 7, 2007
    #2
    1. Advertising

  3. > Know that this is actually rebinding the local variable y to point to a
    > new object, not modifying the old object. If your goal was to modify the
    > array "lines", you should change "lines.each" to "lines.map" and make
    > sure you end the block with a statement that returns the modified y.
    >
    > perhaps:
    > lines.map do |y|
    > y = y.chomp("\x15").chomp
    > y = "<someTag>" + y + "</someTag>" if y =~ /^\*\*\*/
    > y
    > end
    >
    >
    > Dan


    Sorry, actually doing each_with_index do |y,ind| (etc) and specifically
    setting lines[ind] = y
    but my original question remains: how do i make the "<tag>" + y leave
    off the \n so the output is
    <tag>text of y
    Thanks again
    --
    Posted via http://www.ruby-forum.com/.
     
    Gani Ruthellen, Aug 7, 2007
    #3
  4. On Aug 7, 2:04 pm, Gani Ruthellen <> wrote:
    > > Know that this is actually rebinding the local variable y to point to a
    > > new object, not modifying the old object. If your goal was to modify the
    > > array "lines", you should change "lines.each" to "lines.map" and make
    > > sure you end the block with a statement that returns the modified y.

    >
    > > perhaps:
    > > lines.map do |y|
    > > y = y.chomp("\x15").chomp
    > > y = "<someTag>" + y + "</someTag>" if y =~ /^\*\*\*/
    > > y
    > > end

    >
    > > Dan

    >
    > Sorry, actually doing each_with_index do |y,ind| (etc) and specifically
    > setting lines[ind] = y
    > but my original question remains: how do i make the "<tag>" + y leave
    > off the \n so the output is
    > <tag>text of y
    > Thanks again
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Have you considered switching to map?

    As for debugging, I'd try tracking it down using p

    lines = file.readlines("\x15").map do |line|
    line.chomp!("\x15")
    line.sub!(/^\n/, "")
    line = "<someTag>" + line + "</someTag>" if line =~ /^\*{3}/
    p [:line, line] # comment me out after debugging
    line
    end

    p [:lines, lines] # comment me out after debugging

    puts lines
     
    Noah Easterly, Aug 7, 2007
    #4
  5. Gani Ruthellen

    Dan Zwell Guest

    Gani Ruthellen wrote:
    >> Know that this is actually rebinding the local variable y to point to a
    >> new object, not modifying the old object. If your goal was to modify the
    >> array "lines", you should change "lines.each" to "lines.map" and make
    >> sure you end the block with a statement that returns the modified y.
    >>
    >> perhaps:
    >> lines.map do |y|
    >> y = y.chomp("\x15").chomp
    >> y = "<someTag>" + y + "</someTag>" if y =~ /^\*\*\*/
    >> y
    >> end
    >>
    >>
    >> Dan

    >
    > Sorry, actually doing each_with_index do |y,ind| (etc) and specifically
    > setting lines[ind] = y
    > but my original question remains: how do i make the "<tag>" + y leave
    > off the \n so the output is
    > <tag>text of y
    > Thanks again


    Sorry, I thought that would work but didn't test it. "y.lstrip" will do
    what you want, but will also will strip leading whitespace, which might
    not be okay. If not, "y[1..-1]" should suffice--this will return y from
    the second character on.
    y = y[1..-1] if y =~ /^\n/

    Dan
     
    Dan Zwell, Aug 7, 2007
    #5
  6. Noah Easterly wrote:
    > On Aug 7, 2:04 pm, Gani Ruthellen <> wrote:
    >> > end

    >> Posted viahttp://www.ruby-forum.com/.

    > Have you considered switching to map?
    >
    > As for debugging, I'd try tracking it down using p
    >
    > lines = file.readlines("\x15").map do |line|
    > line.chomp!("\x15")
    > line.sub!(/^\n/, "")
    > line = "<someTag>" + line + "</someTag>" if line =~ /^\*{3}/
    > p [:line, line] # comment me out after debugging
    > line
    > end
    >
    > p [:lines, lines] # comment me out after debugging
    >
    > puts lines


    Oh. I did a subset of the p [:line, line] and it looks like i had some
    \n and some \r\n occurring. I was just pulling the \r\n on my sub call
    (wrote it as \n to simplify)because when I was looking at the original
    with end of line showing they all showed as CRLF. Odd.
    Much improved output, and another tool in my meager toolkit. Thanks so
    much, Noah and all other repliers! I'll look at replacing the each with
    map/collect now that I know about it. I do have the Pragmatic
    programming book...
    Cheers, Gani


    --
    Posted via http://www.ruby-forum.com/.
     
    Gani Ruthellen, Aug 7, 2007
    #6
  7. Gani Ruthellen

    dima Guest

    On Aug 7, 9:10 pm, Gani Ruthellen <> wrote:
    > Noah Easterly wrote:
    > > On Aug 7, 2:04 pm, Gani Ruthellen <> wrote:
    > >> > end
    > >> Posted viahttp://www.ruby-forum.com/.

    > > Have you considered switching to map?

    >
    > > As for debugging, I'd try tracking it down using p

    >
    > > lines = file.readlines("\x15").map do |line|
    > > line.chomp!("\x15")
    > > line.sub!(/^\n/, "")
    > > line = "<someTag>" + line + "</someTag>" if line =~ /^\*{3}/
    > > p [:line, line] # comment me out after debugging
    > > line
    > > end

    >
    > > p [:lines, lines] # comment me out after debugging

    >
    > > puts lines

    >
    > Oh. I did a subset of the p [:line, line] and it looks like i had some
    > \n and some \r\n occurring. I was just pulling the \r\n on my sub call
    > (wrote it as \n to simplify)because when I was looking at the original
    > with end of line showing they all showed as CRLF. Odd.
    > Much improved output, and another tool in my meager toolkit. Thanks so
    > much, Noah and all other repliers! I'll look at replacing the each with
    > map/collect now that I know about it. I do have the Pragmatic
    > programming book...
    > Cheers, Gani
    >
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Piece of advice - concatenation in Ruby is very fast but you should
    take the better way by using:
    y = "<someTag>" << y << "</someTag>"
    which is a little faster with same readability ;-)
     
    dima, Aug 7, 2007
    #7
    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 Blair
    Replies:
    1
    Views:
    433
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    456
    Tjerk Wolterink
    Nov 3, 2004
  3. Pat Maddox
    Replies:
    6
    Views:
    170
    Marcin Mielżyński
    Jan 20, 2006
  4. Igor Nn
    Replies:
    7
    Views:
    465
    Johnny Morrice
    May 28, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    153
    PerlFAQ Server
    Jan 26, 2011
Loading...

Share This Page