Tutorial 040 Scramble Text Lines to give Randomised Lists


Following on from the last tutorial, here is a program to randomise the text lines of a possibly long list (found on the net or on disc). As before, "leading spaces" and blank lines are stripped out, then the text lines are saved as strings in an array and a parallel array of initial rank order is randomised using the shuffle method explained in Tutorial 14 . The lines are then printed to the screen. These text lines  can be "lifted" from the screen using SHIFT-TAB (Hold down Shift and Press the TAB key). They are now on the "clipboard" from where they can be pasted into Notepad, Wordpad, Word, email etc.

In particular, this gives a method of sampling a long ordered list, since you can just use the first ten or twenty items of your newly randomised list.


Example :

Here is a text file of 264 punning-names which was produced from the web using the method described in the last tutorial. You can use Select all/copy/paste to lift this text into Notepad, then save the file into the same directory that you will save the main listing. In that way you will be able to load the text file without having to search for it all over your hard disc.

When you run the program and load this text file, you will get a random selection of the names in the output screen such as :


 Press <SPACE> to choose a text file whose lines you wish to randomise

 Full Pathname of this text file :

C:\Programs\RandTextLines\CollectedFunnies.txt

Press SHIFT to scroll to next page

Viv Ascious
Mel Lowe
Mel O'Dramer
Celia Fate
Mike Yermindup
Barnaby Wilde
Duncan Biscett
Phil Maglassop
Percy Cute
Russell Sprout
Herb Altey
Marcus Absent
Edina Cloud
Xavier Money
Ben Detoy
Ian de Deepend
Paul O'Smoke
Warren Peece
Lou Cowt
Phil Itafiche
Lena Meete
Carrie Dowt
Marie Inhaste
Carrie Smattick
Fran Tick
Oliver Sudden

Another time the first screen might come up with  :

Shamus Notfriday
Madge Ority
Eve Ninall
Mal Odruss
Mick Stup
Ian O'Sphere
Colin Derr
Nora Bone
Sergovia Carpet
Ray Sersharpe
Anna Notherthing
Willy Gofarr
Owen Monie
Mollie Codle
Claire Voyant
Ken Ackumin
Mick Stubbles
Fiona Friend
Honour Mission
Tania Hyde
Arthur Ritus
Miss T. Ize
Carrie Micote

Listing :

      REM : Takes a text file, strips leading spaces, strips blank lines
      REM : then randomises the line order
      REM : eg a long alphabetical list from a web page is randomised
      REM : then the first few lines can be culled for an interesting selection
      REM : Richard Weston, 9th July 2003
      MODE 8
      VDU14
      t=500 :REM line capacity
      DIM line$(t),rank(t)
      COLOUR1
      PRINT'" Press <SPACE> to choose a text file whose lines you wish to randomise"
      G=GET
      OFF
      :
      DIM of% 75, ff% 18, fn% 255
      !of% = 76
      of%!4 = @hwnd%
      of%!12 = ff%
      of%!28 = fn%
      of%!32 = 256
      of%!52 = 6
      $ff% = "Text Files"+CHR$0+"*.txt"+CHR$0+CHR$0
      :
      SYS "GetOpenFileName", of% TO result%
      IF result% filename$ = FNnulterm$(fn%)
      COLOUR7
      PRINT'" Full Pathname of this text file :"
      COLOUR2
      PRINT'filename$
      COLOUR6
      PRINT'"Press SHIFT to scroll to next page"'
      :
      fnum=OPENIN filename$
      IF fnum=0 THEN PRINT "No ";filename$;" data": END
      :
      n=0
      temp=0
      COLOUR7
      REPEAT
        line$=""
        REPEAT
          temp=BGET#fnum :REM Read byte
          line$+=CHR$(temp)
        UNTIL temp=10 OR temp=13
        PROCcheckline
        IF printworthy THEN
          n+=1
          line$(n)=line$
          rank(n)=n
        ENDIF
      UNTIL  EOF#fnum
      CLOSE#fnum
      :
      PROClinescramble
      :
      FOR i=1 TO n
        PRINTline$(rank(i))
      NEXT i
      PRINT'" Press<SPACE> to go again..."
      G=GET
      RUN
      END
      :
      DEF FNnulterm$(P%)
      LOCAL A$
      WHILE ?P% <> 0
        A$ += CHR$?P%
        P% += 1
      ENDWHILE
      = A$
      :
      DEF PROCcheckline
      LOCAL i,L,char$,asc
      :
      WHILE LEFT$(line$,1)=" "
        line$=MID$(line$,2) : REM Remove leading spaces
      ENDWHILE
      :
      L=LEN(line$)
      printworthy=FALSE
      FOR i=1 TO L
        char$=MID$(line$,i,1)
        asc=ASC(char$)
        IF asc>32 AND asc<127 THEN printworthy=TRUE
      NEXT i
      ENDPROC
      :
      DEF PROClinescramble
      LOCAL i,temp
      FOR i = n TO 2 STEP -1
        x=RND(i)
        temp=rank(x)
        rank(x)=rank(i)
        rank(i)=temp
      NEXT i
      ENDPROC


Annotated Listing :

      REM : Takes a text file, strips leading spaces, strips blank lines
      REM : then randomises the line order
      REM : eg a long alphabetical list from a web page is randomised
      REM : then the first few lines can be culled for an interesting selection
      REM : Richard Weston, 9th July 2003
      MODE 8
      VDU14 *** Paged mode on ***
      t=500 : REM line capacity *** increase this value for longer lists if you have the full version of BB4W ***
      DIM line$(t),rank(t) *** memory locations for the text lines and the initial and randomised order of them
      COLOUR1
      PRINT'" Press <SPACE> to choose a text file whose lines you wish to randomise"
      G=GET
      OFF *** flashing cursor not wanted ***
      :
      DIM of% 75, ff% 18, fn% 255 ************ Usual stuff  for Windows file loading ..................
      !of% = 76
      of%!4 = @hwnd%
      of%!12 = ff%
      of%!28 = fn%
      of%!32 = 256
      of%!52 = 6
      $ff% = "Text Files"+CHR$0+"*.txt"+CHR$0+CHR$0
      :
      SYS "GetOpenFileName", of% TO result%
      IF result% filename$ = FNnulterm$(fn%) **** End of Windows file loading routine which gets the filename....*****
      COLOUR7
      PRINT'" Full Pathname of this text file :"
      COLOUR2
      PRINT'filename$
      COLOUR6
      PRINT'"Press SHIFT to scroll to next page"'
      :
      fnum=OPENIN filename$ *****...........which filename we can use here to open the file for reading ***
      IF fnum=0 THEN PRINT "No ";filename$;" data": END
      :
      n=0 *** count the printable lines ***
      temp=0 *** to hold the ASC code read later ***
      COLOUR7
      REPEAT ******* Similar to last tutorial's routine ***
        line$=""
        REPEAT
          temp=BGET#fnum : REM Read byte
          line$+=CHR$(temp)
        UNTIL temp=10 OR temp=13
        PROCcheckline
        IF printworthy THEN
          n+=1 *** count a printable line ***
          line$(n)=line$ *** store this line ***
          rank(n)=n *** rank order (to be randomised later) ***
        ENDIF
      UNTIL  EOF#fnum
      CLOSE#fnum
      :
      PROClinescramble *** shuufles the rank oder of the lines ***
      :
      FOR i=1 TO n  *** prints the lines in the randomised order ***
        PRINTline$(rank(i))
      NEXT i
      PRINT'" Press<SPACE> to go again..."
      G=GET
      RUN
      END
      :
      DEF FNnulterm$(P%) *** Required for the loading routine ***
      LOCAL A$
      WHILE ?P% <> 0
        A$ += CHR$?P%
        P% += 1
      ENDWHILE
      = A$
      :
      DEF PROCcheckline *** see last tutorial for explanations ***
      LOCAL i,L,char$,asc
      :
      WHILE LEFT$(line$,1)=" "
        line$=MID$(line$,2) : REM Remove leading spaces
      ENDWHILE
      :
      L=LEN(line$)
      printworthy=FALSE
      FOR i=1 TO L
        char$=MID$(line$,i,1)
        asc=ASC(char$)
        IF asc>32 AND asc<127 THEN printworthy=TRUE
      NEXT i
      ENDPROC
      :
      DEF PROClinescramble *** Shuffles rank order - see Tutorial 14 ***
      LOCAL i,temp
      FOR i = n TO 2 STEP -1
        x=RND(i)
        temp=rank(x)
        rank(x)=rank(i)
        rank(i)=temp
      NEXT i
      ENDPROC


Next Tutorial

Richard Weston's Homepage