Generate Random List of CSV Field Data

This clip will take a selection of data contained in a comma separated file (without quotes) and reorder those fields in a random fashion.

; take delimited comma separated values
; highlight some of them exclusive of outside
; commas one,*two,three,four*,five,six
; it will then put those values at the end
; the file in random order
; by don at htmlfixit.com

; set list delimiter
^!SetListDelimiter ","
; set array = to selection
^!SetArray %values_current_order%=^$GetSelection$

; set new array to same ... will change later
^!SetArray %values_new_order%=^$GetSelection$
; counter to be sure we get all items
^!Set %NA%=0

; in this loop we get a random item from the array
; and check it to see if we already used it
; if not put it in the new array
:Loop
^!Set %random_digit%=^$Calc(RND(^$Calc(^%values_current_order0%-1)$)+1;0)$
; mark out already used values
^!If "^%values_current_order^%random_digit%%" = "*e*m*p*t*y*" Loop ELSE Process

:Process
; build the new array and cycle back for more
; unless done
^!Inc %NA%
^!Set %values_new_order^%NA%%=^%values_current_order^%random_digit%%
^!Set %values_current_order^%random_digit%%=*e*m*p*t*y*
^!If "^%NA%" < "^%values_current_order0%" Loop ELSE Next
^!Jump Doc_End
; you may want to do something else with the output
^!InsertText ^P^P^%values_new_order%^P

Keywords: reorder, random, csv, comma separated value, file

Leave a Reply

Recent Posts

Archives

Topics