Memo to self.

Discussion in 'Ruby' started by John Carter, Feb 20, 2006.

  1. John Carter

    John Carter Guest

    Never under estimate what you can achieve with
    IO.read( "filename").scan(%r{}mx) do |match|
    end

    Especial as you can control how greedy the regex is with .*?

    I just deleted a lot of code and replaced it with one line.

    John Carter Phone : (64)(3) 358 6639
    Tait Electronics Fax : (64)(3) 359 4632
    PO Box 1645 Christchurch Email :
    New Zealand

    Carter's Clarification of Murphy's Law.

    "Things only ever go right so that they may go more spectacularly wrong later."

    From this principle, all of life and physics may be deduced.
     
    John Carter, Feb 20, 2006
    #1
    1. Advertising

  2. John Carter

    Hal Fulton Guest

    John Carter wrote:
    > Never under estimate what you can achieve with
    > IO.read( "filename").scan(%r{}mx) do |match|
    > end
    >
    > Especial as you can control how greedy the regex is with .*?
    >
    > I just deleted a lot of code and replaced it with one line.


    Well, as your self.memo included the rest of us, how about
    sharing the example?


    Hal
     
    Hal Fulton, Feb 20, 2006
    #2
    1. Advertising

  3. John Carter wrote:
    > Never under estimate what you can achieve with
    > IO.read( "filename").scan(%r{}mx) do |match|
    > end
    >
    > Especial as you can control how greedy the regex is with .*?
    >
    > I just deleted a lot of code and replaced it with one line.
    >
    > John Carter Phone : (64)(3) 358 6639
    > Tait Electronics Fax : (64)(3) 359 4632
    > PO Box 1645 Christchurch Email :
    > New Zealand
    >
    > Carter's Clarification of Murphy's Law.
    >
    > "Things only ever go right so that they may go more spectacularly wrong later."
    >
    > From this principle, all of life and physics may be deduced.


    # Read and parse the first file on the command line.
    gets(nil).scan( //mx ) { |s|
    }
    # Read and parse the rest of the files.
    ARGF.read.scan( //mx ) { |s|
    }
     
    William James, Feb 20, 2006
    #3
  4. John Carter

    John Carter Guest

    On Mon, 20 Feb 2006, Hal Fulton wrote:

    > John Carter wrote:
    >> Never under estimate what you can achieve with
    >> IO.read( "filename").scan(%r{}mx) do |match|
    >> end
    >>
    >> Especial as you can control how greedy the regex is with .*?
    >>
    >> I just deleted a lot of code and replaced it with one line.

    >
    > Well, as your self.memo included the rest of us, how about
    > sharing the example?


    Ok. Just quickly. I had a file of the format...

    blah
    blah
    Command :
    some stuff
    some more stuff I \
    want with line continuation \
    characters

    blah
    blah
    Command :
    more some stuff
    more some more stuff I \
    want with line continuation \
    characters

    blah
    blah
    ...

    that I wanted transformed into
    some stuff
    some more stuff I want with line continuation characters
    more some stuff
    more some more stuff I want with line continuation characters


    So I started write a routine to fix up the \ characters yield fixed
    lines and then a state machine to search for 'Command:' and gather
    everything until the blank line and then skip until...

    I start playing with Generator since I realised I could then keep
    everything in a
    each_something do |thing|
    yield other_thing( thing)
    end
    pattern instead of doing state machines and then I threw everything away
    and replaced it with..

    File.read( "big_file").scan(%r{\nCommand\ :\n(.*?)\n\n}mx) do |command|
    puts $1.gsub( %r{\\\n}mx, '')
    end

    and then
    everyone.send( :memo, "to self")

    John Carter Phone : (64)(3) 358 6639
    Tait Electronics Fax : (64)(3) 359 4632
    PO Box 1645 Christchurch Email :
    New Zealand

    Carter's Clarification of Murphy's Law.

    "Things only ever go right so that they may go more spectacularly wrong later."

    From this principle, all of life and physics may be deduced.
     
    John Carter, Feb 23, 2006
    #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. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    601
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    606
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    414
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    385
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    413
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page