Sometimes you have to type out almost identical lines of code, and it’s either time consuming or error prone. Here’s how to avoid it all by using some clever Excel techniques.
You’ll need a spreadsheet
It doesn’t matter which one, I wrote Excel because it’s the most popular and is more likely to appear in search results than Google Sheets, but that’s the one I really used in this example.
Imagine you have a lot of fairly similar code, for example this is the code for the “disassemble” function in my Redcode simulator
This painful looking code does the tedious thing of turning an enum value into a string representation of that enum value (yes, there are more automated ways of doing this, but they’re just plain weird) so it can be printed out. Boring, easy code. Just copy-paste-edit until they’re all done.
Except with a bit of spreadsheet magic you can do the same thing in a few seconds, which is really handy when you then have to type out code like this:
opcodeTable[DAT] = &opDAT; opcodeTable[MOV] = &opMOV; opcodeTable[ADD] = &opADD; opcodeTable[SUB] = &opSUB; opcodeTable[MUL] = &opMUL; opcodeTable[DIV] = &opDIV; opcodeTable[MOD] = &opMOD; opcodeTable[JMP] = &opJMP; opcodeTable[JMZ] = &opJMZ; opcodeTable[JMN] = &opJMN; opcodeTable[DJN] = &opDJN; opcodeTable[CMP] = &opCMP; opcodeTable[SEQ] = &opSEQ; opcodeTable[SNE] = &opSNE; opcodeTable[SLT] = &opSLT; opcodeTable[SPL] = &opSPL;Code language: HTML, XML (xml)
Break out the spreadsheet!
Rather than seeing this as separate rows of text, see it as columns of text, most of which are identical. The only bit that changes is the “DAT”, “MOV”, “ADD” part.
So make your spreadsheet contain those columns…
Notice how there’s a formula in cell D1 to copy the contents of B1, to save re-typing the opcode out.
Next, type out all the opcodes into column B. This takes some effort, but it’s the only time you’ll need to do it
And now, make use of the amazing drag-fill feature built into every spreadsheet program since the mid 90s and watch the code write itself…
Now all you need to do is copy-paste the spreadsheet into your code editor
opcodeTable[ DAT ] = &op DAT opcodeTable[ MOV ] = &op MOV opcodeTable[ ADD ] = &op ADD opcodeTable[ SUB ] = &op SUB opcodeTable[ MUL ] = &op MUL opcodeTable[ DIV ] = &op DIV
Now, the only thing to watch out for is that pasting the code will result in a tab character being put between each cell. This may or may not be what you want. If you don’t like it, just use your editor’s search-and-replace functionality to search for the tab character, and replace it with nothing. Can’t type in a tab character into the search-replace box? Just copy and paste one out the code.
Notice that if that code above was C++ it wouldn’t compile, due to the missing semi-colon? Well fixing that is just a case of adding one to the end of the spreadsheet and dragging it down.