eval without eval
Oct. 28th, 2024 08:46 pmOne of the disadvantages of a small language like awk is the lack of some functions. It's also a great advantage as it is not a gigantic, sprawling language the way many have become nowadays.
Today I encountered the disadvantage, in that I needed something that worked like the eval() function many languages have, that would let me read in a string and execute it. In this case I had a long list of variables and the values I wanted stored into them. I needed to be able read them in to my program.
I tried a few ideas, including a few proposed by ChatGPT-4o. None worked... until I came up with a solution that even GPT-4 hadn't heard of. I made the settings file (named ".canvas") into a fake awk file with
function settings(){
at the top, then a list of the variable assignments, and the last line the closing curly brace.
In my main awk file I have
@include "./.canvas"
near the top of the file and whenever I want to read the variables into my program I simply have
settings()
in my program. Easy peasy. Yay! :D
Today I encountered the disadvantage, in that I needed something that worked like the eval() function many languages have, that would let me read in a string and execute it. In this case I had a long list of variables and the values I wanted stored into them. I needed to be able read them in to my program.
I tried a few ideas, including a few proposed by ChatGPT-4o. None worked... until I came up with a solution that even GPT-4 hadn't heard of. I made the settings file (named ".canvas") into a fake awk file with
function settings(){
at the top, then a list of the variable assignments, and the last line the closing curly brace.
In my main awk file I have
@include "./.canvas"
near the top of the file and whenever I want to read the variables into my program I simply have
settings()
in my program. Easy peasy. Yay! :D