both if and else statements are executed in Stata - if-statement

Both if and else statements are executed when "`md'"=="lowess". Could anyone please explain how in the world this is possible?!
loc methods "connected lowess"
foreach md in `methods' {
if "`md'"=="lowess" {
di "`md'==lowess"
}
else
{
di "`md'!=lowess"
}
}
The output is:
. do "/tmp/SD11749.000000"
. loc methods "connected lowess"
.
. foreach md in `methods' {
2. if "`md'"=="lowess" {
3. di "`md'==lowess"
4. }
5. else
6. {
7. di "`md'!=lowess"
8. }
9. }
connected!=lowess
lowess==lowess
lowess!=lowess

Your else and the subsequent { must be on the same line

Related

C++ and VS: Wrong Formatting after multiple copies of else if block

I noticed a strange behaviour of vs community when copying an else if block with a switch statement in it multiple times.
There is a comment below which claims that it is not reproducible, so if you can reproduce this problem a comment on this would be nice. If nobody can reproduce this behaviour an answer would be great which explains what I am doing wrong.
I am using:
Visual Studio 2019
Windows 10
The Problem:
You copy an else if block with a switch statement in it.
If you paste and change it up to 4 times the formatting works fine when you continue coding.
MyClass::MyMethod()
{
...
if(cond0)
{
...
}
else if(cond1) //that block was copied
{
...
}
else if(cond2) //that block was pasted and changed
{
...
}
.
.
.
else if(cond5) //the last block which was pasted and changed
{
...
}
else //formatting continues to work fine
{
...
}
...
}
but if you paste the else-if block one more time this happens
MyClass::MyMethod()
{
...
if(cond0)
{
...
}
else if(cond1) //that block was copied
{
...
}
else if(cond2) //that block was pasted and changed
{
...
}
.
.
.
else if(cond6) //the last block which was pasted and changed
{
...
}
else
{//here starts the problem: everything after this line too much left
...
}
...
}
Has Someone an idea how to avoid this?

TCL not recognizing C++ Commands NS2

For starters, I had this working a week ago before new software was installed on my computer. Files were previously run with ./FileName.tcl instead of ns FileName.tcl. No idea why that changed, but I have not been able to get NS2 files to run the same again.
I added to the NS2 DumbAgent protocol to get energy of nodes. This was done via the following command in dumbagent.cc:
int DumbAgent::command(int argc, const char*const* argv)
{
if (argc == 3) {
//Other pre-built commands
else if (strcmp(argv[1], "get_energy") == 0)
{
u_int32_t addr = atoi(argv[2]);
get_energy(addr);
Node* iNode = Node::get_node_by_address(addr);
std::cout << iNode << std::endl;
printf("COMMMAND TEST ");
iEnergy = iNode->energy_model()->energy();
std::cout << iEnergy << std::endl;
return TCL_OK;
}
}
return Agent::command(argc, argv);
}
double DumbAgent::get_energy(u_int32_t addr)
{
Node* iNode = Node::get_node_by_address(addr);
std::cout << iNode << "\n";
Tcl& tcl = Tcl::instance();
printf("FUNCTION TEST ");
//addr = addr;
tcl.evalf("puts \"Node Reference from C++: %d \"", addr);
tcl.evalf("puts \"iNode from C++: %i \"", iNode);
double iEnergy;
iEnergy = iNode->energy_model()->energy();
tcl.evalf("puts \"energy = %d\"", iEnergy);
return iEnergy;
}
Using the proper additions to dumbagent.h, object files, and make clean, ./configure/pathtoTCL -withVersion=tcl, make I was able to get all these print statements to spit out what I need - as it took me using all the prints to figure everything out.
However, now, I cannot get anything out. I even added additional prints to the constructor to see what was going on:
DumbAgent::DumbAgent() : Agent(PT_PING) {
printf("TEST TEST TEST");
std::cout << "Constructor called." << std::endl;
iEnergy = 0.0;
bind("energy_otcl", &iEnergy);
bind("addr_otcl", &addr);
}
Nothing - all the DumbAgents and the simulation work. But no output. My Tcl Script calls everything with:
for {set i 0} {$i < $val(nn)} { incr i} {
set dumbagent($i) [new Agent/DumbAgent]
$ns attach-agent $node($i) $dumbagent($i)
}
#$dumbagent(1) get_energy 1
for {set i 0} {$i < $val(nn)} { incr i} {
$dumbagent($i) get_energy $i
puts "$dumbagent($i), $node($i), $i"
}
#$ns at 19.0 "$dumbagent(2) get_energy 2"
Where the first loop, use to print out the constructor items, and the second would print current energy. What is happening here? How can I get my information out?
I know it is calling the correct file because if I completely screw up the dumbagent.cc file, it will not make/configure. I either get no output at all (depending on lines I comment out in Tcl Script) or:
error "error when calling class $cls: $args" $..."
(procedure "_o758" line 2)
(SplitObject unknown line 2)
invoked from within
"$dumbagent($i) get_energy $i"
("for" body line 2)
invoked from within
"for {set i 0} {$i < $val(nn)} { incr i} {
$dumbagent($i) get_energy $i
puts "$dumbagent($i), $node($i), $i"
}"
What is going on?

Check if each string in file have only one word

Input:
[1-st string]Dog
[2-nd string]I love dog
[3-rd string]Dog love me
[4-th string]Love
Output:
[1-st string]Dog
[2-nd string]Love
How can i do such thing in c++ ?>Without using vector.Thx:)
You have a question in the format "No A is within a class B". The algorithm for such problem is
bool wrong = false;
while ( /* there is another A */ )
{
if ( /* check if it is NOT within a class */ )
{
wrong=true;
break;
}
}
if (wrong)
{
// here you know that at least one thing was NOT within a class B
}
else
{
// here you know that every item A was within a class B
}
In your case the "class" is "Strings with no space" and to check if there is space you just use this condition.

'else if' logical statements in gnuplot

The new gnuplot (5.x) has new syntax for logic, but I cannot get the 'else if' statement to work. For example:
if(flag==1){
plot sin(x)
}
else{
plot cos(x)
}
does work, but:
if(flag==1){
plot sin(x)
}
else if(flag==2){
plot cos(x)
}
else if(flag==3){
plot tan(x)
}
does not. I have tried many combinations of {} and placement of 'if' and 'else' to no avail. Does anyone know how to correctly implement 'else if' in gnuplot 5.x?
The gnuplot guide (http://www.bersch.net/gnuplot-doc/if.html) has no examples of the new logic syntax using 'else if' but does have examples using the old syntax, but I would rather avoid the old.
Based on a brief inspection of the source code of command.c in the latest version of Gnuplot, I would say that this feature is not supported. To be more specific, the relevant part can be found on line 1163 (see below). The parser first makes sure that the if is followed by a condition enclosed in parentheses. If the following token is a {, it activates the new syntax, isolates the entire if block enclosed in a pair of matching {} and optionally looks for an else which is however permitted to be followed also only with a {}-enclosed clause. Because of this, a simple script such as:
if(flag == 1){
print 1;
}else if(flag == 2){
print 2;
}
indeed produces the error message expected {else-clause}. One workaround would be to nest the if statements as:
if(flag == 1){
}else{
if(flag == 2){
}else{
if(flag == 3){
}
}
}
which is admittedly slightly more verbose...
void
if_command()
{
double exprval;
int end_token;
if (!equals(++c_token, "(")) /* no expression */
int_error(c_token, "expecting (expression)");
exprval = real_expression();
/*
* EAM May 2011
* New if {...} else {...} syntax can span multiple lines.
* Isolate the active clause and execute it recursively.
*/
if (equals(c_token,"{")) {
/* Identify start and end position of the clause substring */
char *clause = NULL;
int if_start, if_end, else_start=0, else_end=0;
int clause_start, clause_end;
c_token = find_clause(&if_start, &if_end);
if (equals(c_token,"else")) {
if (!equals(++c_token,"{"))
int_error(c_token,"expected {else-clause}");
c_token = find_clause(&else_start, &else_end);
}
end_token = c_token;
if (exprval != 0) {
clause_start = if_start;
clause_end = if_end;
if_condition = TRUE;
} else {
clause_start = else_start;
clause_end = else_end;
if_condition = FALSE;
}
if_open_for_else = (else_start) ? FALSE : TRUE;
if (if_condition || else_start != 0) {
clause = new_clause(clause_start, clause_end);
begin_clause();
do_string_and_free(clause);
end_clause();
}

TCL: loops How to get out of inner most loop to outside?

In the below code once I hit check_access as 0 how do I preserve the value and hit the
if condition below ($check_root && $check_access) . Break will only terminate the inner loop. But the other loops will continue as per me.
} else {
set check_access 0
break
}
}
}
if {$check_root && $check_access} {
set result 1
} else {
set result 0
}
The break and continue operations only go out one level of looping. If you need more than that, consider refactoring so that you can just return. Alternatively, try a custom exception in Tcl 8.6:
try {
foreach a $longList1 {
foreach b $longList2 {
if {[someCondition $a $b]} {
# Custom non-error exception
return -level 0 -code 123
}
}
}
} on 123 {} {
# Do nothing; we're out of the loop
}
break jumps to the end of the innermost loop only, and Tcl has no goto. But return, unless it's inside a catch or similar, exits a procedure which is like jumping to the end of it. So if you make the outermost loop the last command of the procedure (if your code is top-level, you have to put it in a procedure first to be able to use return), you can use return as a multi-break. Just move the commands after the loop out of the procedure and into the caller's code:
proc callMe {} {
foreach ... {
foreach ... {
if ... {
return
}
}
}
# move this code from here...
}
callMe
# ...to here
Another way is to put in extra tests:
set done 0
foreach ... {
foreach ... {
foreach ... {
if ... {
set done 1
break
}
}
if {$done} {break}
}
if {$done} {break}
}