Q:
Hello, script expert! My daughter has a Flash program that can get the word list in a text file. How can I write a script to enable it to open the file and automatically disrupt the order of word lists?
-- SN
A:
Hello, SN. You know, in most cases, we try our best to keep this column focused on actual system management tasks: We tell you how to set default printers, how to disable services, and how to map network drives. However, sometimes we are willing to deal with the question that sounds interesting, and we will not consider the usefulness of the final result. Do system administrators usually need to disrupt the order of word lists in text files? Not required. However, if you only work and do not play, smart children will become silly, right?
In fact, some children are still a bit silly even if they play. But this is another thing.
After clarifying this, let's take a look at the script that can disrupt the word order in text files. As we have said, this kind of work may be of little practical use, but it is a bit challenging and requires a little bit of interesting scripting tips. In addition, if this is not the case, you may never know when such tips will come in handy.
First, assume that you have a text file similar to the following. All words in the file are listed alphabetically:
Apple
Banana
Carrot
Dog
Fish
Elephant
Giraffe
Horse
How can we disrupt the order of these words? Use a script like the following:
Copy codeThe Code is as follows: Const ForReading = 1
Const ForWriting = 2
Set objDictionary = CreateObject ("Scripting. Dictionary ")
Set objFSO = CreateObject ("Scripting. FileSystemObject ")
Set objFile = objFSO. OpenTextFile ("c: \ scripts \ words.txt", ForReading)
I =-1
Do Until objFile. AtEndOfStream
StrLine = objFile. Readline
ObjDictionary. Add strLine, strLine
I = I + 1
Loop
ObjFile. Close
Dim arrWords ()
Redim arrWords (I)
IntWordsLeft = I
Z = 0
Do While intWordsLeft> = 0
Randomize
RndWord = Int (intWordsLeft-0 + 1) * Rnd + 0)
IntWordsLeft = intWordsLeft-1
ColItems = objDictionary. Items
StrText = colItems (rndWord)
ArrWords (z) = strText
Z = z + 1
ObjDictionary. Remove (strText)
Loop
Set objFile = objFSO. OpenTextFile ("c: \ scripts \ words.txt", ForWriting)
For Each strItem in arrWords
ObjFile. WriteLine strItem
Next
ObjFile. Close
Oh, of course, it seems incredible; it is indeed an incredible task (at least for the system administrator ). However, whether you believe it or not, there is logic in it, and we will gradually let you understand this logic.
The first part of the script is indeed quite simple. We define a pair of constants-ForReading and ForWriting. We will use these two constants when processing text files. Create two objects: Scripting. Dictionary and Scripting. FileSystemObject. We will use a Dictionary object as a temporary repository for reading words from a text file, and use FileSystemObject to actually interact with the text file.
Next, use the OpenTextFile method to open the file C: \ Scripts \ Words.txt for reading. (Note the use of the constant ForReading .) Next, create a counter variable I and set its value to-1. We will use this variable to track the number of words in the file. Why do I start from-1 instead of 0? Because we want to use I to create an array, and because the first item in the array is always 0 (instead of 1), we need to start from-1. When we read the first word, I will be set to 0, although it sounds strange, but an array of 0 size means that the array contains a data item.
Hi, we only report the truth. We don't know the reason.
Next is the following code:Copy codeThe Code is as follows: Do Until objFile. AtEndOfStream
StrLine = objFile. Readline
ObjDictionary. Add strLine, strLine
I = I + 1
Loop
What we do here is to read files row by row. For each row in the file (that is, each word), we assign this value to a variable named strLine. Then, we use the Add method to Add this value to the Dictionary object, add the I value to 1. After the file is read, all the words are stored in the Dictionary object, and the I value is 7, which happens to reduce the number of words in the file by 1. (Why? Because the size of the array containing 8 data items is 7 .)
Don't worry; at last you should understand the cause.
We hope so.
After closing the file, initialize the array arrWords and set its size to I (indicating that the number of words in the text file is reduced by 1 ). At the same time, the I value is assigned to the variable intWordsLeft, through which we will know how many words are going to be disrupted. Finally, set the value of the variable z to 0. We will use z to fill the disordered words in the array. In fact, what we need to do is to randomly retrieve a word from the Dictionary and add it to the array. Because words are taken out of the Dictionary in random order, their order in the array is "disrupted" (stored in different order ).
Now it's interesting. Create a Do loop that runs until all words in the Dictionary object are used up. Then use the following two lines of code to subtract 1 from the number of data items in the 0 and Dictionary objects (or at least the actual number of data items, because the first data item in the Dictionary object is 0th) select a random number between them:
Ranndomize
RndWord = Int (intWordsLeft-0 + 1) * Rnd + 0)
Then we subtract the intWordsLeft value by 1. This is to always remember that the word we want to process is less than the one we previously processed.
So what should we do with this random number? What we need to do now is to use this value to randomly extract a word from a Dictionary. Therefore, we can create a set of Dictionary items and store the value of this randomly selected data item number in the variable strText:
ColItems = objDictionary. Items
StrText = colItems (rndWord)
In other words, Banana is currently the 1st item in Dictionary. Assume that 1 is obtained when a random number is generated. This means that we will retrieve the value of the first item from the Dictionary; then, it means that the word Banana is stored in the variable strText.
Do you understand? After a random word is extracted from a Dictionary, We need to store the value somewhere. Therefore, we use the array arrWords to make strText the first entry in the array:
Words (z) = strText
How do we know that this value should be the first item? Because we assign this value to item z, and item z is equal to 0. Then, we immediately add z to 1, and z will be equal to 1. This also means that through the next loop, We will assign the retrieved value to the second item in the array.
After the word Banana is used, the next step is to delete the word from the Dictionary; otherwise, the word may be used again. To delete this word, you only need to call the Remove Method of the Dictionary object and pass the variable strText as the item to be deleted:
ObjDictionary. Remove (strText)
After everything is completed successfully, the array arrWords will contain a list of words that have broken the order from the text file:
Banana
Elephant
Giraffe
Apple
Fish
Carrot
Horse
Dog
Good, right? Then, we only need to open the file Words.txt (this time writing), and then replace the existing content with the list of arrWords that have been disordered:Copy codeThe Code is as follows: Set objFile = objFSO. OpenTextFile ("c: \ scripts \ words.txt", ForWriting)
For Each strItem in arrWords
ObjFile. WriteLine strItem
Next
ObjFile. Close
The next time your daughter (who has the same daughter) runs the education program, what she sees is a random display of words.
By the way, we have not tested the text of this column as people have rumored. Hello, script expert! The words in the column are not randomly selected. Every word is obtained through countless times of hard work and exploration.
As a matter of fact, after all our careful efforts. Our edits randomly disrupt the order of all words. If you can see the appearance of this column before editing, you will be amazed at its beauty! (Edit Note: you will see the situation before editing: You are surprised. I can't use it, but the word is "wonderful ".)