Iterating through a file, sticking iterated array entries in

Discussion in 'Ruby' started by Peter Bailey, Sep 20, 2007.

  1. Peter Bailey

    Peter Bailey Guest

    I've got an array, a listing of "titles" in an xml file. Basically, I
    want to iterate through my file and, wherever I see the tag
    <registration>, I want to replace that with <subhead.4>code</subhead.4>,
    where "code" is the next entry in the stack of my array. Now, I've tried
    this a number of ways and it does it, but, it doesn't cycle through my
    array entries. It populates all of my substitutions with just the first
    entry in the array, none of the subsequent ones.
    ...
    $codes = []
    $codes = xmlfile.scan(/<issueList>\s*<issue +code *="[A-Z]{3}
    *">(.*?)<\/issue>\s*?/mi)
    ...
    $codes.each do |code|
    puts code
    xmlfile.gsub!(/<registration>?/,
    "<subhead.4>#{code}</subhead.4>/n<table>")
    end

    I'm getting the following. My "puts" statement, which is just there for
    checking, does indeed list each entry of the array, though.

    <subhead.4>Trade (Domestic &amp; Foreign)</subhead.4>/n<table>
    which is correct, but, I'm getting it for all 115 occurrences of the
    <registration> tag.

    Maybe gsub isn't the way to do this. Obviously, I'm trying to iterate
    through every instance of <registration> and simply replace it with the
    above code. Is gsub not an iteration tool?

    Thanks a lot,
    Peter
    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Bailey, Sep 20, 2007
    #1
    1. Advertising

  2. On Sep 20, 2007, at 11:41 AM, Peter Bailey wrote:

    > I've got an array, a listing of "titles" in an xml file. Basically, I
    > want to iterate through my file and, wherever I see the tag
    > <registration>, I want to replace that with <subhead.4>code</
    > subhead.4>,
    > where "code" is the next entry in the stack of my array. Now, I've
    > tried
    > this a number of ways and it does it, but, it doesn't cycle through my
    > array entries. It populates all of my substitutions with just the
    > first
    > entry in the array, none of the subsequent ones.
    > ...
    > $codes = []
    > $codes = xmlfile.scan(/<issueList>\s*<issue +code *="[A-Z]{3}
    > *">(.*?)<\/issue>\s*?/mi)
    > ...
    > $codes.each do |code|
    > puts code
    > xmlfile.gsub!(/<registration>?/,
    > "<subhead.4>#{code}</subhead.4>/n<table>")
    > end
    >
    > I'm getting the following. My "puts" statement, which is just there
    > for
    > checking, does indeed list each entry of the array, though.
    >
    > <subhead.4>Trade (Domestic &amp; Foreign)</subhead.4>/n<table>
    > which is correct, but, I'm getting it for all 115 occurrences of the
    > <registration> tag.
    >
    > Maybe gsub isn't the way to do this. Obviously, I'm trying to iterate
    > through every instance of <registration> and simply replace it with
    > the
    > above code. Is gsub not an iteration tool?


    The gsub! matches all the substitution targets on the first iteration
    of the 'each'; therefore, no matches occur on any subsequent
    iterations. Try something like:

    <code>
    source = <<TEXT
    The quick brown
    <fox> jumped over
    the lazy <dog>
    The quick brown
    <fox> jumped over
    the lazy <dog>
    TEXT

    codes = %w[a b c d e f g]
    source.gsub!(/<(\w*)>/) { c = codes.shift; "<#{c}>#{$1}</#{c}>"}
    puts source
    </code>

    <results>
    The quick brown
    <a>fox</a> jumped over
    the lazy <b>dog</b>
    The quick brown
    <c>fox</c> jumped over
    the lazy <d>dog</d>
    </results>

    Regards, Morton
     
    Morton Goldberg, Sep 21, 2007
    #2
    1. Advertising

  3. Peter Bailey

    Peter Bailey Guest

    Re: Iterating through a file, sticking iterated array entrie

    Morton Goldberg wrote:
    > On Sep 20, 2007, at 11:41 AM, Peter Bailey wrote:
    >
    >> ...
    >> I'm getting the following. My "puts" statement, which is just there
    >> above code. Is gsub not an iteration tool?

    > The gsub! matches all the substitution targets on the first iteration
    > of the 'each'; therefore, no matches occur on any subsequent
    > iterations. Try something like:
    >
    > <code>
    > source = <<TEXT
    > The quick brown
    > <fox> jumped over
    > the lazy <dog>
    > The quick brown
    > <fox> jumped over
    > the lazy <dog>
    > TEXT
    >
    > codes = %w[a b c d e f g]
    > source.gsub!(/<(\w*)>/) { c = codes.shift; "<#{c}>#{$1}</#{c}>"}
    > puts source
    > </code>
    >
    > <results>
    > The quick brown
    > <a>fox</a> jumped over
    > the lazy <b>dog</b>
    > The quick brown
    > <c>fox</c> jumped over
    > the lazy <d>dog</d>
    > </results>
    >
    > Regards, Morton


    Thanks a lot, Morton. Yes, I've modified my approach. I just had to
    expand my mind to really consider how to contain each discrete thing
    that I want in these huge text files I'm getting. Once I've got them all
    in an array, I see now that I can do stuff to each entry in the array.
    Exactly what I need. Your suggestion helped. Thanks again.
    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Bailey, Sep 21, 2007
    #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. Pierre Thibault
    Replies:
    10
    Views:
    451
    Pierre Barbier de Reuille
    Jul 28, 2006
  2. Don Bruder
    Replies:
    3
    Views:
    1,018
    spikeysnack
    Aug 3, 2010
  3. Kyle Barbour
    Replies:
    10
    Views:
    607
    Marvin Gülker
    Aug 2, 2010
  4. Replies:
    9
    Views:
    142
    juglesh
    Mar 4, 2005
  5. Replies:
    9
    Views:
    165
Loading...

Share This Page