variable-assignment

Why would you use an assignment in a condition?

天涯浪子 提交于 2019-11-26 02:09:59
问题 In many languages assignments are legal in conditions. I never understood the reason behind this. Why would you write: if (var1 = var2) { ... } instead of: var1 = var2; if (var1) { ... } 回答1: It's more useful for loops than if statements. while( var = GetNext() ) { ...do something with var } Which would otherwise have to be written var = GetNext(); while( var ) { ...do something var = GetNext(); } 回答2: I find it most useful in chains of actions which often involve error detection, etc. if (

Assign multiple new variables on LHS in a single line

青春壹個敷衍的年華 提交于 2019-11-26 02:07:00
问题 I want to assign multiple variables in a single line in R. Is it possible to do something like this? values # initialize some vector of values (a, b) = values[c(2,4)] # assign a and b to values at 2 and 4 indices of \'values\' Typically I want to assign about 5-6 variables in a single line, instead of having multiple lines. Is there an alternative? 回答1: There is a great answer on the Struggling Through Problems Blog This is taken from there, with very minor modifications. USING THE FOLLOWING

Why does this go into an infinite loop?

China☆狼群 提交于 2019-11-26 02:03:08
问题 I have the following code: public class Tests { public static void main(String[] args) throws Exception { int x = 0; while(x<3) { x = x++; System.out.println(x); } } } We know he should have writen just x++ or x=x+1 , but on x = x++ it should first attribute x to itself, and later increment it. Why does x continue with 0 as value? --update Here\'s the bytecode: public class Tests extends java.lang.Object{ public Tests(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object.\"<init>

Multiple left-hand assignment with JavaScript

五迷三道 提交于 2019-11-26 01:56:52
问题 var var1 = 1, var2 = 1, var3 = 1; This is equivalent to this: var var1 = var2 = var3 = 1; I\'m fairly certain this is the order the variables are defined: var3, var2, var1, which would be equivalent to this: var var3 = 1, var2 = var3, var1 = var2; Is there any way to confirm this in JavaScript? Using some profiler possibly? 回答1: Actually, var var1 = 1, var2 = 1, var3 = 1; is not equivalent to: var var1 = var2 = var3 = 1; The difference is in scoping: function good() { var var1 = 1, var2 = 1,

Assign multiple columns using := in data.table, by group

穿精又带淫゛_ 提交于 2019-11-26 01:51:40
问题 What is the best way to assign to multiple columns using data.table ? For example: f <- function(x) {c(\"hi\", \"hello\")} x <- data.table(id = 1:10) I would like to do something like this (of course this syntax is incorrect): x[ , (col1, col2) := f(), by = \"id\"] And to extend that, I may have many columns with names stored in a variable (say col_names ) and I would like to do: x[ , col_names := another_f(), by = \"id\", with = FALSE] What is the correct way to do something like this? 回答1:

Why do C and C++ support memberwise assignment of arrays within structs, but not generally?

a 夏天 提交于 2019-11-26 01:35:14
I understand that memberwise assignment of arrays is not supported, such that the following will not work: int num1[3] = {1,2,3}; int num2[3]; num2 = num1; // "error: invalid array assignment" I just accepted this as fact, figuring that the aim of the language is to provide an open-ended framework, and let the user decide how to implement something such as the copying of an array. However, the following does work: struct myStruct { int num[3]; }; struct myStruct struct1 = {{1,2,3}}; struct myStruct struct2; struct2 = struct1; The array num[3] is member-wise assigned from its instance in

Assign output of a program to a variable using a MS batch file

我与影子孤独终老i 提交于 2019-11-26 01:24:23
问题 I need to assign the output of a program to a variable using a MS batch file. So in GNU Bash shell I would use VAR=$(application arg0 arg1) . I need a similar behavior in Windows using a batch file. Something like set VAR=application arg0 arg1 . 回答1: One way is: application arg0 arg1 > temp.txt set /p VAR=<temp.txt Another is: for /f %%i in ('application arg0 arg1') do set VAR=%%i Note that the first % in %%i is used to escape the % after it and is needed when using the above code in a batch

Javascript AND operator within assignment

为君一笑 提交于 2019-11-26 01:23:47
问题 I know that in JavaScript you can do: var oneOrTheOther = someOtherVar || \"these are not the droids you are looking for...\"; where the variable oneOrTheOther will take on the value of the first expression if it is not null , undefined , or false . In which case it gets assigned to the value of the second statement. However, what does the variable oneOrTheOther get assigned to when we use the logical AND operator? var oneOrTheOther = someOtherVar && \"some string\"; What would happen when

Why do C and C++ support memberwise assignment of arrays within structs, but not generally?

不羁的心 提交于 2019-11-26 01:07:53
问题 I understand that memberwise assignment of arrays is not supported, such that the following will not work: int num1[3] = {1,2,3}; int num2[3]; num2 = num1; // \"error: invalid array assignment\" I just accepted this as fact, figuring that the aim of the language is to provide an open-ended framework, and let the user decide how to implement something such as the copying of an array. However, the following does work: struct myStruct { int num[3]; }; struct myStruct struct1 = {{1,2,3}}; struct

How do I do multiple assignment in MATLAB?

♀尐吖头ヾ 提交于 2019-11-26 00:44:02
问题 Here\'s an example of what I\'m looking for: >> foo = [88, 12]; >> [x, y] = foo; I\'d expect something like this afterwards: >> x x = 88 >> y y = 12 But instead I get errors like: ??? Too many output arguments. I thought deal() might do it, but it seems to only work on cells. >> [x, y] = deal(foo{:}); ??? Cell contents reference from a non-cell array object. How do I solve my problem? Must I constantly index by 1 and 2 if I want to deal with them separately? 回答1: You don't need deal at all