variable-assignment

How to assign hash['a']['b']= 'c' if hash['a'] doesn't exist?

做~自己de王妃 提交于 2019-12-17 06:38:10
问题 Is there any way simpler than if hash.key?('a') hash['a']['b'] = 'c' else hash['a'] = {} hash['a']['b'] = 'c' end 回答1: The easiest way is to construct your Hash with a block argument: hash = Hash.new { |h, k| h[k] = { } } hash['a']['b'] = 1 hash['a']['c'] = 1 hash['b']['c'] = 1 puts hash.inspect # "{"a"=>{"b"=>1, "c"=>1}, "b"=>{"c"=>1}}" This form for new creates a new empty Hash as the default value. You don't want this: hash = Hash.new({ }) as that will use the exact same hash for all

Difference between a += 10 and a = a + 10 in java? [duplicate]

為{幸葍}努か 提交于 2019-12-17 05:07:09
问题 This question already has answers here : Why don't Java's +=, -=, *=, /= compound assignment operators require casting? (11 answers) Closed 3 years ago . Are a += 10 and a = a + 10 both the same, or is there some difference between them? I got this question while studying assignments in Java. 回答1: As you've now mentioned casting... there is a difference in this case: byte a = 5; a += 10; // Valid a = a + 10; // Invalid, as the expression "a + 10" is of type int From the Java Language

How to assign to a global variable in Sass?

梦想与她 提交于 2019-12-17 04:35:08
问题 I run this Sass code: $a: 1; @if 2 + 2 == 4 { $a: 2; } @debug $a; I expect to see 2. The output, however, is: Line 5 DEBUG: 1 I understand that Sass creates a new $a variable inside the @if scope. How can I change this behaviour and assign a value to the global $a ? I use Sass 3.4.0. 回答1: As you're using Sass 3.4+, you should append the !global flag to your variable declaration: $a: 1; @if 2 + 2 == 4 { $a: 2 !global; } @debug $a; // will output 2 The original SASS_REFERENCE on variable

Why is `i = ++i + 1` unspecified behavior?

别说谁变了你拦得住时间么 提交于 2019-12-17 04:20:23
问题 Consider the following C++ Standard ISO/IEC 14882:2003(E) citation (section 5, paragraph 4): Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. 53) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to

How can I change the values of multiple points in a matrix?

夙愿已清 提交于 2019-12-17 03:44:12
问题 I have a matrix that is [500x500]. I have another matrix that is [2x100] that contains coordinate pairs that could be inside the first matrix. I would like to be able to change all the values of the first matrix to zero, without a loop. mtx = magic(500); co_ords = [30,50,70; 30,50,70]; mtx(co_ords) = 0; 回答1: You can do this using the function SUB2IND to convert your pairs of subscripts into a linear index: mtx(sub2ind(size(mtx),co_ords(1,:),co_ords(2,:))) = 0; 回答2: Another answer: mtx(co_ords

How to assign a value to a TensorFlow variable?

别说谁变了你拦得住时间么 提交于 2019-12-17 03:34:23
问题 I am trying to assign a new value to a tensorflow variable in python. import tensorflow as tf import numpy as np x = tf.Variable(0) init = tf.initialize_all_variables() sess = tf.InteractiveSession() sess.run(init) print(x.eval()) x.assign(1) print(x.eval()) But the output I get is 0 0 So the value has not changed. What am I missing? 回答1: In TF1, the statement x.assign(1) does not actually assign the value 1 to x , but rather creates a tf.Operation that you have to explicitly run to update

Array Type - Rules for assignment/use as function parameter

让人想犯罪 __ 提交于 2019-12-17 02:33:14
问题 when I need to pass an array to a function, it seems all the following declarations of the function will work void f(int arr[]) void f(int arr[4]) // is this one correct? for this: int a[]={1,2,3,4}; f(a); But when I assign an array to another array, it fails int a[]={1,2,3,4}; int b[4] = a; // error: array must be initialized with a brace-enclosed initializer So why an array passed as an argument of a function is okay, but used on the rhs of simple assignment is wrong? 回答1: For understanding

How to assign from a function which returns more than one value?

你说的曾经没有我的故事 提交于 2019-12-16 23:01:15
问题 Still trying to get into the R logic... what is the "best" way to unpack (on LHS) the results from a function returning multiple values? I can't do this apparently: R> functionReturningTwoValues <- function() { return(c(1, 2)) } R> functionReturningTwoValues() [1] 1 2 R> a, b <- functionReturningTwoValues() Error: unexpected ',' in "a," R> c(a, b) <- functionReturningTwoValues() Error in c(a, b) <- functionReturningTwoValues() : object 'a' not found must I really do the following? R> r <-

scala zip list to tuple

孤人 提交于 2019-12-14 04:17:13
问题 Working with JodaTime, trying to convert a List[LocalDate] to Tuple2[JodaTime, JodaTime] so I can do multi-assigment like so: val(expire, now) = List(row.expireDate, new JodaDate) zip (_.toDateTimeAtStartOfDay.getMillis) which of course does not compile. Is there a similarly concise way to do the above? I know I can just do it manually: val(expire, now) = (row.expireDate.toDateTimeAtStartOfDay.getMillis, new JodaDate().toDateTimeAtStartOfDay.getMillis) but that's a bit ugly 回答1: val Seq

Storing text and numeric variable from file to use in perl script

我是研究僧i 提交于 2019-12-14 04:10:27
问题 I am trying to prepare a bash script for use with gnu parallel. The script should take a filename, store the prefix of the file name as a describer, and store the row count (wc -l) as a numeric variable. Both if these become variables to use in a perl script. The describer is working fine. But my storage of the number of rows, or my use of ${mm} is not generating a numeric variable that the perl script recognises. Any corrections appreciated. #!/bin/bash # Get the filename and strip suffix