How to Assign the Result of the Previous Expression to a Variable

How to assign the result of the previous expression to a variable?

.Last.value is an answer.

It was answered once but you have better title.

How to assign the result of an expression to a variable in bash

I thought that wrapping an expression in $(...) gave the functionality

You did not wrap enough. Wrap the whole expression, including echo.

cn=$(echo "$json_data" | jq '.country_name')

Remember to quote variable expansions. Check your script with shellcheck - shellcheck will notify you of many problems.

You can put anything inside $(...), maybe this will clear it:

cn=$(
echo "$json_data" | jq '.country_name'
)

R: Get last result from console

yes there is, it is .Last.value:

> 4*5
[1] 20
> .Last.value * 5
[1] 100

How do I assign the result of a function to a variable?

In the shell, passing values is done through the output, not the return code:

#!/bin/bash

MaxArray() {
local n aux="$1"
for n in "$@"
do
(( n > aux )) && aux=$n
done
echo "$aux"
}

value=$( MaxArray "${array[@]}" )

retroactively assign the output of a function to a variable in python

I'm assuming you are in the Python interactive interpreter, and that you haven't actually used _ to echo it's value! If you did, then the output of your function call is lost.

If you did not echo anything else, but bound _ before, then that's a new global shadowing the interactive interpreter built-in that captured your function output.

Use del _ to remove it and reveal the built-in, that still will hold your function output:

>>> del _
>>> output = _

This works because the built-in _ name is always bound to the last expression result regardless of there being a global _:

>>> _ = 'foo'
>>> 1 + 1 # an expensive operation!
2
>>> del _
>>> _
2

If you are using IPython, then you can access all results by their output number; if a result is echoed with Out[42] in front, then use _42 to access the cached result.

Assigning the result of 'test' to a variable

As others have documented here, using the string "true" is a red herring; this is not an appropriate way to store boolean values in shell scripts, as evaluating it means dynamically invoking a command rather than simply inspecting the stored value using shell builtins hardcoded in your script.

Instead, if you really must store an exit status, do so as a numeric value:

[ -f "$file" ]               # run the test
result=$? # store the result

if (( result == 0 )); then # 0 is success
echo "success"
else # nonzero is failure
echo "failure"
fi

Assign result of block to a variable (gives SyntaxError)

As you've seen in the Node REPL, a block evaluates to a value, and that value is usually the value of the last statement in the block.

In ECMAScript 6.0, a BlockStatement is defined as follows (with subscripts omitted for simplicity):

BlockStatement:
Block

Block:
{ StatementList }

StatementList:
StatementListItem
StatementList StatementListItem

StatementListItem:
Statement
Declaration

Per section 13.2.13, Note 2, "The value of a StatementList is the value of the last value producing item in the StatementList." A Block is evaluated to the value of its StatementList, and a BlockStatement is evaluated to the value of its Block.

Thus, Node is correctly evaluating your BlockStatement to the value of the last value producing item, which is a. This is not a bug, nor is it specific to Node.

The reason you are getting an error is because you are trying to use a BlockStatement in a context where it is not allowed.

const result = { let a = 1 + 2; a }; is a LexicalDeclaration, which is defined as follows (again, with subscripts omitted):

LexicalDeclaration:
LetOrConst BindingList ;

LetOrConst :
let
const

BindingList:
LexicalBinding
BindingList , LexicalBinding

LexicalBinding:
BindingIdentifier Initializer
BindingPattern Initializer

We also need the definition of Initializer:

Initializer:
= AssignmentExpression

As you can see, the part of your lexical declaration after the equal sign requires an AssignmentExpression. If you look through the grammar for expressions, you will see that BlockStatement is not an AssignmentExpression.

As of ES6.0, there is no way to use a BlockStatement direcly as an AssignmentExpression. However, if you want to evaluate one to an expression, you can use eval:

> const result = eval("{ let a = 1 + 2; a }");
> result
3

I don't recommend doing this under ordinary circumstances, but it is helpful for seeing how ES6.0 does indeed evaluate blocks to values.

How do i assign value to a variable with a result of block of codes containing multiple if's in java8 using lamda expression

Actually this is in Java 8 (as well as Java 7 etc.):

public class HelloWorld {
public static void main(String[] args) {
final String x = (args.length > 0) ? args[0] : "mydefault";
System.out.println(x);
}
}

final String x = (args.length > 0)? args[0]:"default"; is pretty functional since it's expression-oriented, side-effects free and immutable.


If you insist on "functional Java 8" you can do also

public class HelloWorld {
public static void main(String[] args) {
final String x = Stream.of(args).findFirst().orElse("mydefault");
final Consumer<String> cons = System.out::println;
cons.accept(x);
}
}

but I guess this would be overkill.



Related Topics



Leave a reply



Submit