Random section of text file

PythonPython Forum LeaderThe Royal RAM
Lets say Ive got a text file which has got 10000 paragraphs in it each seperated by a blank line.

How can I have a script which randomly selects 200 of those paragraphs and puts it into a variable? Each time the script is reloaded the script should select different paragraphs...

The Royal Ram

Comments

  • ChroderChroder Senior Member The Royal RAM
    You could use explode() to get each paragraph by iteself, then just use mt_rand() to get a random paragraph from the array. But considering there are 10,000 of them -- this isn't very efficient.

    First, you'll have to count how many paragraphs there are by using substr_count. Then make a loop that'll go around 200 times (for each one you want), and gets a random number between 1 and the integer returned from substr_count.

    Then you'll have a random number that'll be used to represent which paragraph of the 10,000 you want to get. You can then use various string functions (getting the position of each blank line with strpos) until you get to the part you need, and using more string functions (like substr) you can extract it.
  • PythonPython Forum Leader The Royal RAM
    Oh... errrrmm... hmmmm :confused:

    The Royal Ram

  • ChroderChroder Senior Member The Royal RAM
    On second thought, if you already have 10,000 paragraphs loaded into memory (that is, in a $var), then exploding it into an array shouldn't be much more intensive then it already is.

    If it needed to be fast and light, you should either 1) read the file in maybe 1024 byte increments (getting the paragraphs as you go along) or 2) store the paragraphs separately somehow (perhaps in a database)
  • PythonPython Forum Leader The Royal RAM
    Ok thanks :D

    The Royal Ram

Sign In or Register to comment.