Stuff I Didn’t Publish This Year
December 13, 2006 Hey, Ted
This is regarding your recent posting titled “Quick Query over a Database File” that was published on August 23, 2006. This is truly a useless utility. There already is a RUNQRY command. Why would you post this as something that someone might be interested in? I use to wait anxiously for the next Four Hundred Guru e-mail and get really good things out of it, but this and some of the other ones posted are really not Guru material in my opinion. Next time maybe you can show them something like this: RUNQRY and press F4. –Greg Greg is absolutely right. That tip and some of the others that I have published are not of Guru caliber. So why would I publish such things? I’ll answer that question in a minute, but first I’d like to share with you some things I didn’t publish. 1. Swapping Values Did you know that it is possible to swap the values of two similarly-defined variables without a temporary holding variable? This code fragment illustrates that technique. D Var1 s 8a D Var2 s 8a /free Var1 = 'ARE GOOD'; Var2 = 'MY GOATS'; Var1 = %bitxor(Var1: Var2); Var2 = %bitxor(Var1: Var2); Var1 = %bitxor(Var1: Var2); Believe it or not, after this code runs, Var1 has the value “MY GOATS” and Var2 has the value “ARE GOOD”. Do you know why it works? Here’s a hint: Given three strings–two original strings and their exclusive or–you can derive any of the three strings from the other two. I think we could consider this variable-swapping tip an advanced technique, since beginning business programmers normally don’t learn about bit-twiddling. In fact, I doubt many intermediate or advanced RPG programmers know this technique. By that criterion, I propose that this technique qualifies for Guru status. But I never published it because I can’t find any practical value in this tip. If you saw this code fragment, would it be obvious to you that the two variables were exchanging values? If there were no explanatory comment, could you figure out what was going on? I couldn’t. I believe it is better to avoid such “clever” code and use a temporary swapping variable. I think that using a swapping variable would be easier to understand, and to be on the safe side, I’d comment it anyway. D Var1 s 8a D Var2 s 8a D Temp s 8a /free Var1 = 'ARE GOOD'; Var2 = 'MY GOATS'; // swap the values of Var1 and Var2 Temp = Var1; Var1 = Var2; Var2 = Temp; 2. Another Way to Derive the Exclusive Or This bit-twiddling reminds me of another piece of abstruse information. Did you know that you can derive an exclusive or by using other bit operations? It’s true. Here’s the formula. A XOR B = NOT (A AND B) AND (A OR B) Therefore the following assignment statements are equivalent to one another: C = %bitxor(A:B); C = %bitand(%bitnot(%bitand(A:B)): %bitor(A:B)); I suppose this is an example of the kinds of facts all gurus should know, and that all aspiring gurus would want to know. It might come in handy if I were working in a language that didn’t support an exclusive or operation. But since RPG, OPNQRYF, and SQL have exclusive or functions, where’s the need for this technique? 3. Ignoring Extra Blanks in SQL Commands Did you happen to know that SQL doesn’t mind extra spaces around qualification delimiters? In the following example, the period is used to qualify column field names in the SELECT clause, and the slash qualifies the file name in the FROM clause. select c.order, c.Warehouse, c.Line, c.comment, c.destination from mylib/SomeFile as c Here’s the same code with extra blanks. select c.order, c .Warehouse, c. Line, c . comment, c . destination from mylib / SomeFile as c I’ve never run this as a tip, because I couldn’t figure out what difference it made. Is this knowledge useful? 4. Roman Numerals How about a routine to build Roman numerals? Do you suppose such routines merit Guru status? I wrote the logic for this routine in pseudocode on a scrap of paper one day while watching one of my sons receive swimming instructions for Boy Scouts. The only practical value I’ve found for this routine is practical jokes. Email someone a report with all numerals–even the run date and page number–expressed in Roman format, then wait for your phone to ring. “Are you sure you wanted Arabic numerals?” you ask the caller. “I could have sworn you said you wanted the report in Roman numerals.” I also developed the algorithm to convert Roman numerals to Arabic while waiting to meet my dad in a store one day. The resulting subprocedure is in the code referred to by the last link. Am I getting anywhere near Guru level yet? 5. Have Computer, Will Sudoku Does it take a guru to write a program that solves Sudoku puzzles? I wrote such a program when I got stuck while trying to solve a Sudoku puzzle. I wondered if perhaps the puzzle had more than one solution, so I decided to write a program to find out. I also wondered if RPG were able to handle the challenge, so I decided to write the program in RPG. My program has some academic interest in that it has two subprocedures–DoDigit and Try–that call one another. That is, DoDigit calls Try, which calls DoDigit, which calls Try, which calls DoDigit, which calls Try, and so on, down to 163 levels of call stack. It prints all possible solutions to a puzzle. I never published the program, because I doubt that employers are clamoring for Sudoku-solving computer programs. 6. Linked Lists During an odd moment I pondered the question, “Is it possible to implement linked lists in RPG?” After all, I had found linked lists to be useful in Pascal programs back in the olden days when I was younger and pursuing my computer science degree. Here is an exploratory program I wrote to pursue that idea. I concluded that it is possible to write RPG code to implement linked lists, but it is not practical. I leave it to the interested reader to develop the idea farther. Answering Greg’s Question Enough of that esoteric stuff. Why did I publish the QF command? For one reason: Based on e-mail I had received from readers of this newsletter, I thought some of you would find it useful. I get e-mail from people who are out there on the leading edge, but the overwhelming bulk of it comes from people who are trying to solve everyday problems. Among the questions I was asked recently are “What is a prototype?” and “How do I get the code you published from my PC to the iSeries?” I answer some of these requests by providing a link to a site that answers the question at hand. I answer others with a short explanation and maybe some sample code. For every question I answer, there are many that I never answer. It’s not that I’m important, but that I’m busy trying to make a living and raise a family. I just don’t have much time. During 2007, I hope to address the needs of iSeries professionals of all skill levels. I will do my best to provide a good mix of practical tips. I invite anyone whose finds little or nothing of value in these pages to send me some tips that they deem of value. If we think they are of value, we’ll publish them. |