0 votes
by (8.9k points)

Does anybody know a way to automatically modify a plain text file like this:

James
Lars
Kirk
Cliff
Dave
Jason
Robert

Into the format where I could just paste it into a Twine array:

"James",
"Lars",
"Kirk",
"Cliff",
"Dave",
"Jason",
"Robert"

 

2 Answers

+1 vote
by (960 points)
selected by
 
Best answer

Any text or code editor that supports finding expressions should work for this. I'll use Notepad++ as an example.

Go to Search > Replace... and at the bottom of the window you'll see a "Search Mode" section. Select "Regular expression" which will allow you to use special characters to detect certain elements, like ^ being the start of a line and $ being the end.

If you're only doing a part of the file, you can either select the lines tick the "In selection" button or just cut and paste them into a new file to work with (I find using the "In selection" kinda finnicky personally).

In the "Find what:" box, type ^ which will grab the start of each line. In the "Replace with:" box, type " so that each line will start with a quotation mark. Click "Replace All".

Then find and replace the character $ with ", (quotation mark and a comma) which will add a quotation mark and a comma to the end of every line. Click "Replace All".

Then just go to the last line and delete the final comma and add your declaration and brackets. Here's a video demonstration. Learning regular expressions is an extremely useful tool for anyone who works with code.

by (8.9k points)
Perfect, this is the easiest way for me to do this.  Thanks for the help.  :-)
+1 vote
by (44.7k points)

For things like that I tend to just build a macro in Notepad++.

In your example, I'd:

  1. have my cursor at the start of the first line
  2. pick: "Macro" -> "Start Recording" from the menu
  3. hit: "
  4. hit: the END key
  5. hit: ",
  6. hit: the → key
  7. pick: "Macro" -> "Stop Recording" from the menu
  8. hit: CTRL+SHIFT+P repeatedly (to play the macro you just built) until after you hit it on the last line
  9. remove the extra comma

Notepad++ is a handy little tool for just about any developer's toolkit.

Enjoy!  :-)

by (8.9k points)
Thanks HiEv, that's very useful!
...