Looping Filenames in Stata error - stata

I am aware that there already exist related threads like how do I loop through file names in stata
I follow those instructions but however receive the invalid syntax r(198) error in Stata.
My code looks as follows:
foreach var of "*/ABC.dta" ABC{
infix observation 1-2 date 3 using "*/CC_ABC`var'.txt"
save "*/CC_ABC`var'.dta" ,replace
}
Where ABC.dta is a list of pretty random numbers which all occur in file names. Do you have an idea why I get errors here?
Thanks a lot!

Is this real code?
Stata wouldn't get past the lack of a listtype following of. This is the very first thing explained in the help for foreach.
If this isn't real code, please show us a real example of something that doesn't work, ideally one that we can reproduce.
(If you don't understand something, imposing your own abstraction or generalization is just likely to muddy your question for those who do.)

Related

syntax command syntax issue

I've spent hours reading Stata help file (17SE) but I'm unable to understand why the syntax in this syntax command is wrong:
syntax, n(integer) interact(real) infage(integer min=45 max=75) supage(integer min=45 max=75)
For sure the part until interact(real) works, but what's wrong with the following part?
I suspect that you want something more like
syntax, n(integer) interact(real) infage(numlist integer >=45 <=75) ///
supage(numlist integer >=45 <=75)
There are two points here.
The min and max arguments are about how many elements are specified, not about what their values might be. I doubt that you want to insist that the user types in at least 45 integers to each option.
Expecting that a specification integer() would support specification of the allowed range seems reasonable enough, but nothing in the documentation supports that. It's numlist() that allows more checking.

Maxima: creating a function that acts on parts of a string

Context: I'm using Maxima on a platform that also uses KaTeX. For various reasons related to content management, this means that we are regularly using Maxima functions to generate the necessary KaTeX commands.
I'm currently trying to develop a group of functions that will facilitate generating different sets of strings corresponding to KaTeX commands for various symbols related to vectors.
Problem
I have written the following function makeKatexVector(x), which takes a string, list or list-of-lists and returns the same type of object, with each string wrapped in \vec{} (i.e. makeKatexVector(string) returns \vec{string} and makeKatexVector(["a","b"]) returns ["\vec{a}", "\vec{b}"] etc).
/* Flexible Make KaTeX Vector Version of List Items */
makeKatexVector(x):= block([ placeHolderList : x ],
if stringp(x) /* Special Handling if x is Just a String */
then placeHolderList : concat("\vec{", x, "}")
else if listp(x[1]) /* check to see if it is a list of lists */
then for j:1 thru length(x)
do placeHolderList[j] : makelist(concat("\vec{", k ,"}"), k, x[j] )
else if listp(x) /* check to see if it is just a list */
then placeHolderList : makelist(concat("\vec{", k, "}"), k, x)
else placeHolderList : "makeKatexVector error: not a list-of-lists, a list or a string",
return(placeHolderList));
Although I have my doubts about the efficiency or elegance of the above code, it seems to return the desired expressions; however, I would like to modify this function so that it can distinguish between single- and multi-character strings.
In particular, I'd like multi-character strings like x_1 to be returned as \vec{x}_1 and not \vec{x_1}.
In fact, I'd simply like to modify the above code so that \vec{} is wrapped around the first character of the string, regardless of how many characters there may be.
My Attempt
I was ready to tackle this with brute force (e.g. transcribing each character of a string into a list and then reassembling); however, the real programmer on the project suggested I look into "Regular Expressions". After exploring that endless rabbit hole, I found the command regex_subst; however, I can't find any Maxima documentation for it, and am struggling to reproduce the examples in the related documentation here.
Once I can work out the appropriate regex to use, I intend to implement this in the above code using an if statement, such as:
if slength(x) >1
then {regex command}
else {regular treatment}
If anyone knows of helpful resources on any of these fronts, I'd greatly appreciate any pointers at all.
Looks like you got the regex approach working, that's great. My advice about handling subscripted expressions in TeX, however, is to avoid working with names which contain underscores in Maxima, and instead work with Maxima expressions with indices, e.g. foo[k] instead of foo_k. While writing foo_k is a minor convenience in Maxima, you'll run into problems pretty quickly, and in order to straighten it out you might end up piling one complication on another.
E.g. Maxima doesn't know there's any relation between foo, foo_1, and foo_k -- those have no more in common than foo, abc, and xyz. What if there are 2 indices? foo_j_k will become something like foo_{j_k} by the preceding approach -- what if you want foo_{j, k} instead? (Incidentally the two are foo[j[k]] and foo[j, k] when represented by subscripts.) Another problematic expression is something like foo_bar_baz. Does that mean foo_bar[baz], foo[bar_baz] or foo_bar_baz?
The code for tex(x_y) yielding x_y in TeX is pretty old, so it's unlikely to go away, but over the years I've come to increasing feel like it should be avoided. However, the last time it came up and I proposed disabling that, there were enough people who supported it that we ended up keeping it.
Something that might be helpful, there is a function texput which allows you to specify how a symbol should appear in TeX output. For example:
(%i1) texput (v, "\\vec{v}");
(%o1) "\vec{v}"
(%i2) tex ([v, v[1], v[k], v[j[k]], v[j, k]]);
$$\left[ \vec{v} , \vec{v}_{1} , \vec{v}_{k} , \vec{v}_{j_{k}} ,
\vec{v}_{j,k} \right] $$
(%o2) false
texput can modify various aspects of TeX output; you can take a look at the documentation (see ? texput).
While I didn't expect that I'd work this out on my own, after several hours, I made some progress, so figured I'd share here, in case anyone else may benefit from the time I put in.
to load the regex in wxMaxima, at least on the MacOS version, simply type load("sregex");. I didn't have this loaded, and was trying to work through our custom platform, which cost me several hours.
take note that many of the arguments in the linked documentation by Dorai Sitaram occur in the reverse, or a different order than they do in their corresponding Maxima versions.
not all the "pregexp" functions exist in Maxima;
In addition to this, escaping special characters varied in important ways between wxMaxima, the inline Maxima compiler (running within Ace editor) and the actual rendered version on our platform; in particular, the inline compiler often returned false for expressions that compiled properly in wxMaxima and on the platform. Because I didn't have sregex loaded on wxMaxima from the beginning, I lost a lot of time to this.
Finally, the regex expression that achieved the desired substitution, in my case, was:
regex_subst("\vec{\\1}", "([[:alpha:]])", "v_1");
which returns vec{v}_1 in wxMaxima (N.B. none of my attempts to get wxMaxima to return \vec{v}_1 were successful; escaping the backslash just does not seem to work; fortunately, the usual escaped version \\vec{\\1} does return the desired form).
I have yet to adjust the code for the rest of the function, but I doubt that will be of use to anyone else, and wanted to be sure to post an update here, before anyone else took time to assist me.
Always interested in better methods / practices or any other pointers / feedback.

Simple Code Optimisation

I am a quite new programmer, and I sometimes have really dumb questions,
In a few weeks I am supposed to give back this big semester project and I would have liked a bit of help for my optimization.
Somewhere I needed to get a Quantitiy (class derived from a double) and strip it down to a just a number without integers and print it in a window (I dont have the slightest clue how the latter works, it was given to us by the teacher, but it's not the problem here).
And so I created two variables to do so, which gave me something like this:
int lil_patate=q_nutriments;
string patate(to_string(lil_patate));
And I would have like to set that in a single line, writing that;
string patate(to_string(int lil_patate=q_nutriments));
which of course doesnt work, as I expected, but I would have loved a bit of help to get something working that would be simpler than the first version but doing the same thing,
Thanks for the help and have a nice day :)
Humphrey
If you need to be able to reference lil_patate elsewhere in your code then you can't make this factorisation at all. If you don't need to refer to lil_patate elsewhere then get rid of it and initialise patate directly from q_nutrients:
string patate(to_string(q_nutriments));
However, while this may improve the readability of the code, it doesn't represent an optimisation in any technical sense.

C++ Variables from external file using ifstream

I've written a program in VB which exports data into a text file, which I want to be read by another program in C++ and then that data be assigned as a variable. The program is essentially a quiz and the program I wrote in VB is question maker that exports all the data required into the text file.
Below is the text file:
test question|test A|test B|test C|test D|A|100|0|0|0|Right, I know this. The answer is 100% A. Good luck!|B|100|0|
From left to right we have, the question, AnswerA, AnswerB, AnswerC, AnswerD, the correct answer, percent 1, 2, 3, 4, (for polling an audience of what they think the answer may be) a friend's answer, a wrong answer (the program eliminates two wrong answers at one point, this answer is the remaining wrong answer) and percent 1 and 2 again (in case they eliminate 2 wrong answers but still want a poll).
I did quite a lot of google searching but found nothing to my following question (perhaps due to the fact I used the wrong keywords, I'm not too educated when it comes to programming jargon). What I want the C++ program to do is read the file and when it sees "|" it knows that what is coming is a new variable. Would I be better using ifstream or something else and how would I tell the program to identify the "|" and make it read whatever is in between as a variable?
Look at the documentation for istream::getline. You can use the | character as a delimiter.

How do I know when a variable is accessed within my code?

I'm using VS2008 to write a program. There's one specific line in my code that causes a numerical error. It is:
Qp[j] = (Cp - Cm)/(Bp + Bm);
Qp is a std::vector. When I comment this line out, the numerical error disappears. I am going through my code line by line to find all the places that access Qp[j]. I was wondering if there was a feature in VS2008 or a linux program that wraps around the executable that can identify every line of code that reads from that section of memory (the specific element in the vector)?
I tried searching online but the keywords I used brought up results relating to global variables.
--- EDIT
Hi all. To those have responded, thank you. Just to clarify my question:
Imagine I have a vector with 5 elements. I'd like to know all the places in my code that use the value stored in element 3 at any point in time during execution. Is there an easy way to do this?
I am not sure if I understand you correctly, but if you comment out that line and the code works then maybe the problem is that line, and you don't need to check others lines.
Maybe in your case you get in the situation where Bp+Bm = 0 (division by zero error).
Qp may not have as many elements as the index j, check the size of Qp.