in reply to To regex or not to regex

{local $/="====\n"; while( <> ){ open (NEW, "> $old_file$..txt") || die $!; #open output chomp; print NEW; } }

Replies are listed 'Best First'.
Re: Re: To regex or not to regex
by abstracts (Hermit) on Mar 11, 2002 at 06:46 UTC
    This code is slightly broken because it ignores a requirement that was specified by the problem (every section starts with a line containing a number which is the number of records) and relies on an assumption instead (every section is terminated by ==== followed by "\n"). There are at least three cases where this would break, and they do happen more often than not.
    • When there is a record ending with "====", this code breaks the section into more files than it should.
    • When there is a space after the "====", then it won't match the input record separator, thus 2 sections will be merged in one file. This space would not be visible to the eye so debugging this problem is not easy.
    • When there is an empty line after the last "====", you will have an additional empty file.
    So, the wisdom behind this story: follow the specs carefully and don't golf when you don't need to.

    Hope this helps,,,

    Aziz,,,

      Just realized that your post was targeted at Anonymous' post and not mine. DUH!!!!
      That said, I almost used '====' as the separator, but my first test went into a weird loop, so I did not continue along that path.

      mndoci

      "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'

      Yes it does rely on that assumption. I did have lines in my code that removed leading whitespace, but I didn't think about trailing whitespace. As for records between the # and '====', they will nto have any '====', so I guess I am safe, but I agree, my solution is not general. Thanks for the warning!!

      mndoci

      "What you do in this world is a matter of no consequence. The question is, what can you make people believe that you have done?"-Sherlock Holmes in 'A study in scarlet'