variable-assignment

JavaScript variable undefined vs not defined

≯℡__Kan透↙ 提交于 2019-11-28 10:03:34
I have an HTML page with the following JavaScript attached. alert(box); box = "Thinking outside the box"; In the console I get "Uncaught ReferenceError: box is not defined" when I change it to: alert(box); var box = "Thinking outside the box"; The alert gets called and shows undefined. I need to be able to explain this, I have a vague idea of why this happens. I know that when I use var, JavaScript knows the variable is there before the alert is executed, but has not necessarily assigned a value to it?? Am I way off here? Need some help understanding this. jfriend00 When you define a variable

Is a += b more efficient than a = a + b in C?

岁酱吖の 提交于 2019-11-28 10:02:26
I know in some languages the following: a += b is more efficient than: a = a + b because it removes the need for creating a temporary variable. Is this the case in C? Is it more efficient to use += (and, therefore also -= *= etc) So here's a definitive answer... $ cat junk1.c #include <stdio.h> int main() { long a, s = 0; for (a = 0; a < 1000000000; a++) { s = s + a * a; } printf("Final sum: %ld\n", s); } michael@isolde:~/junk$ cat junk2.c #include <stdio.h> int main() { long a, s = 0; for (a = 0; a < 1000000000; a++) { s += a * a; } printf("Final sum: %ld\n", s); } michael@isolde:~/junk$ for

Are multiple variable assignments done by value or reference?

大兔子大兔子 提交于 2019-11-28 09:36:33
$a = $b = 0; In the above code, are both $a and $b assigned the value of 0 , or is $a just referencing $b ? With raw types this is a copy. test.php $a = $b = 0; $b = 3; var_dump($a); var_dump($b); Output : int(0) int(3) With objects though, that is another story (PHP 5) test.php class Obj { public $_name; } $a = $b = new Obj(); $b->_name = 'steve'; var_dump($a); var_dump($b); Output object(Obj)#1 (1) { ["_name"]=> string(5) "steve" } object(Obj)#1 (1) { ["_name"]=> string(5) "steve" } Regard this code as: $a = ($b = 0); The expression $b = 0 not only assigns 0 to $b , but it yields a result as

Is variable assignment and reading atomic operation?

安稳与你 提交于 2019-11-28 09:36:16
I was unable to find any reference to this in the documentations... Is assigning to a double (or any other simple type, including boolean) an atomic operation viewed from the perspective of threads? double value = 0; public void First() { while(true) { value = (new Random()).NextDouble(); } } public void Second() { while(true) { Console.WriteLine(value); } } In this code sample, first method is called in one thread, and the second in another. Can the second method get a messed up value if it gets its execution during assignment to the variable in another thread? I don't care if I receive the

Ruby variable assignment in a conditional “if” modifier

与世无争的帅哥 提交于 2019-11-28 09:32:52
问题 I have a question about how the Ruby interpreter assigns variables: I use this quite often: return foo if (foo = bar.some_method) where some_method returns an object or nil. However, when I try this: return foo if (true && (foo = bar.some_method)) I get: NameError: undefined local variable or method foo for main:Object. What is the difference in evaluation between the first and second lines that causes the second line to error? 回答1: Read it carefully : Another commonly confusing case is when

SET a variable in SELECT statement - MySQL

给你一囗甜甜゛ 提交于 2019-11-28 09:17:54
I'm using this code which has an error: SET @rejects = ''; SELECT * FROM list WHERE maker = 1 AND by_ids IN ('10','11') AND country LIKE '%I%' AND ( src IS NULL || src NOT IN (@rejects) AND checkSrc(src) = 'yes' AND SET @rejects = CONCAT(@rejects,',',src) ); What's causing the issue? The issue is that you cannot mix select and set in one statement, there'll surely be syntax error: select*from t where 1 and set@a=1; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set@a=1' at line 1 If you

Pointer initialisation gives segmentation fault

这一生的挚爱 提交于 2019-11-28 08:46:08
I wrote a C program as follows: CASE 1 int *a; /* pointer variable declaration */ int b; /* actual variable declaration */ *a=11; a=&b;/* store address of b in pointer variable*/ It gives a segmentation fault when running the program. I changed the code as follows: CASE 2 int *a; /* pointer variable declaration */ int b; /* actual variable declaration */ a=&b;/* store address of b in pointer variable*/ *a=11; Now it's working fine. If anyone knows please explain why it is giving a segmentation fault in CASE 1. CASE .1 int *a; /* pointer variable declaration */ int b; /* actual variable

Dynamic global variables assignment

雨燕双飞 提交于 2019-11-28 08:37:14
问题 I'm new in python and I'm having many troubles in using global instruction. Here is a code example: mouse = "a" background = "b" list_ab = [mouse, background] def func (): for item in list_ab: global item # I want to modify the GLOBAL item, which is mouse # and background. item = "modified" print mouse # must be "modified" not "a" print background # must be "modified" not "b" This is the problem. How can I solve it? 回答1: Your problem is that Python works not the way you think. So I will try

Manipulating matrix elements in tensorflow

ぐ巨炮叔叔 提交于 2019-11-28 08:37:04
How can I do the following in tensorflow? mat = [4,2,6,2,3] # mat[2] = 0 # simple zero the 3rd element I can't use the [] brackets because it only works on constants and not on variables. I cant use the slice function either because that returns a tensor and you can't assign to a tensor. import tensorflow as tf sess = tf.Session() var1 = tf.Variable(initial_value=[2, 5, -4, 0]) assignZerosOP = (var1[2] = 0) # < ------ This is what I want to do sess.run(tf.initialize_all_variables()) print sess.run(var1) sess.run(assignZerosOP) print sess.run(var1) Will print [2, 5, -4, 0] [2, 5, 0, 0]) dga You

What does assigning a literal string to an NSString with “=” actually do?

眉间皱痕 提交于 2019-11-28 07:30:12
问题 What does the following line actually do? string = @"Some text"; Assuming that "string" is declared thusly in the header: NSString *string; What does the "=" actually do here? What does it do to "string"'s reference count? In particular, assuming that for some reason "string" is not otherwise assigned to, does it need to be released? Thanks! 回答1: The assignment is just that. The string pointer is basically a label that points to specific address in memory. Reassignment statement would point