logo

Pro Tip: Quickly Convert Text in to a Programmatic Array or List

Every now and then I need to turn a plain text list in to a useable list for my code. For example, I want to turn this plain list:

Monday
Tuesday
Wednesday
Thursday
Friday

In to something like this:

String[] weekDays = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

For small lists like that a manual process is fine. However, for large lists it's too just much to do it by hand.

That's when I normally use my favourite text editor, TextPad, for the job.

Let's say we have a list of American state codes and their corresponding names, which we want to turn in to a list of strings by surrounding each with quotation marks and a delimiter. That's, like, doing the same thing 100 times. Instead you can simple use Regualar Expressions in a power-editor's Replace dialog and have it done in a matter of seconds with no risk of repetitive strain injury.

In the shot below I've pasted my list in to Textpad and then opened the Replace dialog.

image

Notice I've told it I'm looking for regular expression matches and that I'm searching for $ which in regex parlance is the end of a line. Hitting Replace All then puts a quote mark and a colon at the end of each line, as below:

image

You can see how easy this is already, right?

In the shot above I've kept the Replace dialog open and told it to look for ^ (start of line) and replace it with a ", which leads to the list below:

image

Nearly done. Now, in the shot above, I'm looking for all carriage returns (new lines) and replacing them with nothing. Which leads to the list below:

image

It's now ready to paste straight in to your code!!

The above example was for a Notes @Formula list, but it's obviously very simple to adapt to any type of language.

This was a simple example of what can be even more powerful when you get your head round using RegEx in a string search/replace pattern.

 

Comments

  1. Much as I love Textpad... Sublime? http://www.sublimetext.com/

      • avatar
      • Jake Howlett
      • Fri 13 Jul 2012 05:54 AM

      Wow! How did I not know about that!?

      I see the demo video on the homepage shows an even quicker way to convert weekday names in to a list. Doh!

      Thanks, downloading now...

    • avatar
    • ipinky7
    • Fri 13 Jul 2012 07:44 AM

    you call this a ProTip?

    why not do it really fancy...

    and just use 1 step!

    you can search for (.*)\n which will find all text until the end of the line

    you can replace with "$1"\n (sometimes, depending on the editor $1 has to be replaced with \1

    then you only need one step!

    You can actually create full code with this technique! We use it to create code to import from Excel

    source (can be easily created, by concatenating columns, in Excel and even maintained by a customer for easy maintenance) is something like:

    columnId@@@Fieldname@@@FieldType

    resulting source code is a list of Types

    Type importField

    columnId as string

    fieldName as string

    fieldType as string ' can be used for conversions if needed

    end Type

    dim importFields list as importField

    'from here it's the auto generated code as per technique explained above

    set thisImportField = new importField

    thisImportField.columndId = "A"

    thisImportField.fieldName = "Firstname"

    thisImportField.fieldType = "String"

    that way you can create an import setup script with 500 lines in 15 min

  2. So I didn't do it in one step but I did use the column edit in NotePad++ (PC only free editor).

    Here's a sort video I made illustrating both the column edit and the regexp versions.

    http://screencast.com/t/yriZK206

      • avatar
      • Jake Howlett
      • Fri 13 Jul 2012 02:39 PM

      Loving the screencast Rob! Especially liked how it was addressed to me throughout. I feel as though we've finally met :-)

      I should have known I'd end up with egg on my face when I called something a "pro tip", without thinking there'd be much easier and more elegant way to do it, as you demonstrated so well (more RobShaver screencasts I say!).

      I guess the tip I wanted to get across was that there are easier ways. Which way you choose doesn't matter. Whether you can do it with 3 regexps or 1 is irrelevant as they're both a zillion times quicker than hand editing.

      That said, I know I'll be using the column editing approach from now on. I'm a new convert to Sublime Text, as mentioned by Dragon above.

      Show the rest of this thread

      • avatar
      • Chris C
      • Wed 18 Jul 2012 04:28 PM

      I've been using Notepad++ for a few years now and didn't know about the column editing. Thanks for the tip!

      Notepad++ is great. It's free and it's open source. It can't get much better than that, except for maybe being cross platform, which it unfortunately is not (I'd love an OS X version).

      http://notepad-plus-plus.org/

      Show the rest of this thread

    • avatar
    • Mark Teichmann
    • Sat 14 Jul 2012 04:55 AM

    For more complex Things I Love to use PowerShell but with Excel you can also automate boring tasks using dragging technique. Especially complex SQL queries are easily built in Excel (e.g. adding single rows to a table including Roll-back scripts.

    1. I also use excel a lot with Concatenate() and such. But SublimeText is impressive, I must say...

  3. Just a couple of points...

    First, you can do a lot with @formula, so I've sometimes used a 'temp' form with a couple of text-list fields on it, and a simple text-field to enter a clump of @formula, and a button to @evaluate it. It can then grab a pasted block of text from the 'input' field and write it out to the 'output' field. Then copy and paste theat to wheres it needed. The @formula uses some 'tricky' list-processing, but I find it simpler and more predictable than reg-ex's!

    Alternatively, you can use reg-ex's in LotusScript easy enough. You load in a COM component thats on all windows machines courtesy of 'VBScript' - heres some code:

    'init RegExp Object

    dim re as variant

    Set re = createobject("VBScript.Regexp")

    If IsEmpty(re) Then

    Call agLog.Logerror(999, "Unable to init (Regexp) Object on machine," & _

    " check VBScript.Regexp (vbscript.dll) registry/registration." & _

    " regExp validation inoperable")

    Error 999

    End If

    You can then use your 're' object according to MS rules, eg, set a match pattern: re.pattern("") and checking for a match: re.match (which you need to enumurate over) or re.test or re.replace(""). The object does support numbered replace groups and global replace and case sensitivity on/off. Works well in practice.

Your Comments

Name:
E-mail:
(optional)
Website:
(optional)
Comment:


About This Page

Written by Jake Howlett on Fri 13 Jul 2012

Share This Page

# ( ) '

Comments

The most recent comments added:

Skip to the comments or add your own.

You can subscribe to an individual RSS feed of comments on this entry.

Let's Get Social


About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »

More Content