|Up: Later Abominations||[Related] «^» «T»|
Saturday, January 23, 1999
By Paul Ford
Hello. I'd like to share with you a complex variable in which I take great pride. It's from the Ftrain parser.
A word of context: the Ftrain parser builds complex data structures from a preset XML document, organizing all Ftrain content in a Document->Book->Chapter->Entry structure. This structure uses a combination of arrays, or one-dimensional lists (to preserve order) and hashes, or two-dimensional records (to create records). Unfortunately, I don't know a better way to deal with the problem of XML than with Perl. This will change when I learn more about the Document Object Model, which turns documents into structured, standard information, and XQL, which gives XML database functionality. But I didn't want to wait and learn, or work my way around the buggy Perl DOM and XQL modules.
So here I was. I had an array (Book) which contained hashes. An element in each hash was an array of chapters, which in turn contained hashes, and again contained an element which was an array. In the past, I would have written a function that made another hash, an easier one to work with, one that stood alone, and use that to cross-index the documents. But I don't like redundancy, and I don't like setting, and later undefining, global variables. That's why I built up a large list-of-lists-and-hashes in the first place, right? I wanted to have one large data structure in memory, and access it procedurally. This is, after all, a computer, not a card catalog.
In my program, I needed to find the last member of the entry array in the previous chapter. This piece of information is essential for including the "previous" link on every web page, and to have "previous" work across the boundary of chapters. In my program and my documents, entries are walled in by chapters.
So, in a fever, I wrote (all on one line):
After about 35 tweaks, adjusting brackets and operators and running the script over and over, sometimes fetching nothing, sometimes getting the right answer every other time, I added a final $#, and the script began to fetch the right piece of information, exactly on schedule.
There it was--a perfect "previous entry" link, leaping across chapters. I felt as if I had grown a short notch as a scripter. I had peered into a complicated data structure, a gnarl of interconnected information so intricate that only a theologian could fathom it, and made it perform my desire.
I'm sure there's a way to shorten my hundreds of lines of code into two or three map and grep statements, and make it run in a fiftieth of the time. Guys like Randal Schwartz and Tom Christiansen could rewrite my program in seven minutes and have it do thirty times as much. But--and it's hard to put into words--when I look at that impossibly complicated single variable, it's hard not to feel the smallest burst of pride in understanding, and enjoy the small sense of mastery it yielded.