Reuse lines of code in Stata, Similar to JavaScript function? - stata

I have some lines of code in a Stata do-file that I would like to reuse/execute and different points in the do file. Similar to a JavaScript function... however I do not necessarily need an input variable.
For example, I have some code:
*code to reuse
foreach x in test1 test2 test3{
rename variable_`x' variablenew_`x'
}
and I want to execute those 3 lines of code at various points in the do file. As if there was a way to label those three lines codeA and then execute codeA later in the do file.
Any suggestions?

Check help program.
An example program (that takes no arguments):
// define program
capture program drop hello
program hello
display "hello world!"
end
// try it out
hello

Related

Commands in GWBASIC

I am using GWBASIC and cannot figure out a few things. Like, when I'm saving a program after running it with F4, it says: File not found.
Secondly, when I'm using auto command it shows * with line numbers.
Finally, if I want to take program and its output's print on paper, what should I do?
I am using GWBASIC and cannot figure out a few things. Like, when I'm
saving a program after running it with F4, it says: File not found.
Try saving like this:
SAVE"myprog.bas",a
Secondly, when I'm using auto command it shows * with line numbers.
The star (*) on a line means that line already exists and you are overwriting it.
If you use the command NEW to wipe-out the program from memory before running 'auto', you won't see those stars on lines.
Finally, if I want to take program and its output's print on paper,
what should I do?
1) Save the program as a text file:
SAVE"myprog.bas",a
2) Open the file 'myprog.bas' with a text editor (like Notepad++).
3) Print it.
To print a GW-BASIC program, use the LIST command.
The optional , filename parameter specifies the output for the listing. It could be to a file (e.g. to dump the text so another program can load it), or it could be to a printer device (e.g. LPT1:).
So this should work:
LIST ,LPT1:
See also https://robhagemans.github.io/pcbasic/doc/1.2/#LIST

Running a C++ script inside a Matlab loop

I have a C++ function that given 3 integers and a string, makes some statical tests from a matrix read from a txt file and creates as output a txt file called as the input string.
I would like to place this program in a matlab scrips that loops inside a cell array A made of sparse matrices in the following way:
formatSpec = string('Validated_edge_layer%d_time%d');
for k=1:size(A,1)
for j=1:size(A,2)
n1=size(A{k,j},1);
n2=size(A{k,j},2);
e=nnz(A{k,j});
write_to_txt(A{k,j};
string=sprintf(formatSpec,k,j);
*** HERE I WOULD LIKE TO CALL THE C++ FUNCTION WITH INPUTS n1,n2,string ***
end
end
So basically inside the loop matlab evaluates the inputs of the C++ function and writes within each iteration a txt files that is then read by the C++ function (the files gets overwritten so that the C++ function can read a new set of variables). At the end of the loop I would like to have something like 120 txt files each named differently and in an order that follows the cell array organization.
Is this task possible? I know that something called MEX files can be used but I know nothing about it.
I hope that I have explained myself clearly. Thanks.

Piping to provide a file as input to a C program

I have this set of .gz files and inside each of them is a single text file. This text file needs to be used in a C program. The following code solves this problem somehow where parameters 1 and 2 are integers which I'm receiving as arguments for the C program (argc, argv[]) in main().
gzip -dc xyz.txt.gz | ./program parameter1 parameter2
Can someone explain how the above code works in command line?
How does the text file automatically get passed to the program?
Do I need to write extra code in the C program to receive this text file?
The shell connects the stdout of one command directly to the stdin of the other command through a pipe(7). Neither program has to do anything out of the ordinary to take advantage of this.

Simple D program Output order is wrong

I am learning a new language called "D" but i have a problem when trying to write a simple program
import std.stdio;
void main()
{
double gradeOne;
writeln("Please enter the First Test Grade: ");
readf(" s", &gradeOne);
}
Why does my program ask me for the input first before the output message?
I think its just the DDT problem, when i run the program in command prompt its working fine
Output to Eclipse buffers output by larger data blocks rather than lines. To force output to appear, insert calls to stdout.flush(); before asking for input to ensure it shows up when you want it.
See also: Eclipse console writes output only after the program has finished

Output of one c++ file as input of the other?

I have a two C++ source code in which one code generates an array for the specified input while other has to use array for execution. I want to know how can I link two C++ file such that output of the first file is the input for second one ?
Since they're separate programs, that means they each have a main() function. Because of that you can't link them together. What you can do, however, is use the shell to redirect the output from one program to the input of another. For example:
program1 | program2
The above creates a so-called "pipe". What it does is feed program2 with the output of program1. Only the standard input and standard output are redirected that way. In C++ that means std::cin and std::cout. Anything printed on std::cerr or std::clog is not redirected, so make sure to never print errors, warnings or other status/informational messages on std::cout. Only print the payload data and use std::cerr or std::clog for anything else.
Linux: Compile both files and push the content of the first to the second binary with a pipe in terminal else use a socket.. you can try to ouput the data with a binary-stream and the second binary can use the same techniqe to pushs it into a array.. i hope that helps you..