[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Better way to interpret $N as dollars, not macro argument?
From: |
Daniel Goldman |
Subject: |
Better way to interpret $N as dollars, not macro argument? |
Date: |
Wed, 13 Aug 2014 23:16:33 -0700 |
User-agent: |
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 |
I ran into a problem where $N is (of course) interpreted as $N macro
argument, but I wanted dollar amount. Here is the actual code with the
problem output, extracted into a separate m4 file, and with most array
elements removed to simplify the example:
-----------------------
$ cat dollar-03.m4
m4_changequote([[[,]]])
m4_define(RNG_NameArray_Lo_______RNG_VN_Income_Level_______,
[[[
"0", "$10,000", "$75,000"
]]])
m4_define(RNG_NameArray_Hi_______RNG_VN_Income_Level_______,
[[[
"$9,999", "$14,999", "$75,000+"
]]])
RNG_NameArray_Lo_______RNG_VN_Income_Level_______
RNG_NameArray_Hi_______RNG_VN_Income_Level_______
-----------------------------
$ m4 -P dollar-03.m4 | grep -v "^$"
"0", ",000", ",000"
",999", ",999", ",000+"
-------------------------------
To fix the problem, after several other tries, this is the best I came
up with, which later I also saw in section 5.3 of the GNU m4 manual:
$ cat dollar-04.m4
m4_changequote([[[,]]])
m4_define(RNG_NameArray_Lo_______RNG_VN_Income_Level_______,
[[[
"0", "[[[$]]]10,000", "[[[$]]]75,000"
]]])
m4_define(RNG_NameArray_Hi_______RNG_VN_Income_Level_______,
[[[
"[[[$]]]9,999", "[[[$]]]14,999", "[[[$]]]75,000+"
]]])
RNG_NameArray_Lo_______RNG_VN_Income_Level_______
RNG_NameArray_Hi_______RNG_VN_Income_Level_______
--------------------------------
$ m4 -P dollar-04.m4 | grep -v "^$"
"0", "$10,000", "$75,000"
"$9,999", "$14,999", "$75,000+"
-----------------------------
This works, but having to mark up each dollar sign with quote delimiters
seems quite awkward to me. The reason I moved away from cpp was because
of such awkward "special cases" as this. As a general pre-processor, I
don't think m4 should have to take special measures with dollar amounts,
which are easily going to occur in source code (this case) and in
general documents, and could be easily missed by the user (as happened
in this case to my embarrassment). It's a significant bother to have to
keep a lookout for dollar amounts and specially mark them up. Also,
looking for dollar amounts to enclose in quote delimiters would not be
trivial, even with automation, because the user (such as me) would have
to look at each instance by hand to tell if "macro argument" or "dollar
amount". BTW, this post is related to a previous discussion, April 2014,
about named arguments. There seemed to be agreement m4 named arguments
are desirable. This problem I ran into is I think another significant
disadvantage of $N arguments. Also, I can't (even if I wanted to) use
HTML entity for $ because the application is not limited to HTML.
Anyway, my question: Is there a better way to tell m4 not to interpret
$N as "dollar amount"? Based on the manual, the answer seems "no". But I
thought I would ask, hoping maybe some other way...
Thanks,
Daniel
- Better way to interpret $N as dollars, not macro argument?,
Daniel Goldman <=