Assigning String with Boolean Expression

Assigning string with boolean expression

The ternary boolean expression works as:

>>> 2 and 3 or 4
3
>>> 0 and 3 or 4
4

So, this expression:

openmode = IS_PY2 and 'w' or 'wt'

Become in Python 2:

openmode = True and 'w' or 'wt'

Which is equivalent to

openmode = 'w' or 'wt'

So, i gives w.

Under Python 3, IS_PY2 is False, giving:

openmode = False and 'w' or 'wt'

Which is equivalent to

openmode = False or 'wt'

Giving wt.


All of this is to specify explicitely that the openmode is for text files, not binary, which is indicated by w in Python2 and wt in Python3.

While the Python3 t mode is the default one, this is not necessary to precise it.

See this answer about wt mode.


Finally, i think that the following is much more readable:

openmode = 'w' if IS_PY2 else 'wt'

And this one, much more simple:

openmode = 'w'

How to Convert a String Variable to a Boolean Variable in Java?

if(s) can take boolean expressions (and use boolean operators, such as or). For example, String.equals(Object) (or String.equalsIgnoreCase(String)). Something like,

if (string.equals("A") || string.equals("a")) {
// ...
} else if (string.equalsIgnoreCase("B")) {// <-- or equalsIgnoreCase(String).
// ...
}

What boolean value return assign integer or string to a variable

Your expression is:

if ( (@ref := `wsf_ref`) and (@type := `type`), 1, 1)

MySQL does not necessarily evaluate both conditions. It only needs to evaluate the "second" one if the "first" evaluates to true. (I put "first" and "second" in quotes because the order of evaluation is not determined, but the idea is the same regardless.)

When these are strings, the result of @ref := wsf_rf is a string. The string is converted to a boolean, via a number. The value is 0 -- which is false -- unless the string happens to start with digit.

Hence, both conditions are not evaluated and the second is not assigned.

I would write this as:

SELECT t.*,
(@rn := if(@tr = CONCAT_WS(':', wsf_ref, type),
@rn + 1,
if(@tr := CONCAT_WS(':', wsf_ref, type), 1, 1
)
)
) as rn
FROM (SELECT t.*
FROM t
ORDER BY `wsf_ref`, `type`, `wsf_value` DESC
) t CROSS JOIN
(SELECT @rn := 0, @tr := '') params;

I moved the ORDER BY to a subquery because more recent versions of MySQL don't handle ORDER BY and variables very well.

Convert string to boolean in C#

I know this is not an ideal question to answer but as the OP seems to be a beginner, I'd love to share some basic knowledge with him... Hope everybody understands

OP, you can convert a string to type Boolean by using any of the methods stated below:

 string sample = "True";
bool myBool = bool.Parse(sample);

// Or

bool myBool = Convert.ToBoolean(sample);

bool.Parse expects one parameter which in this case is sample, .ToBoolean also expects one parameter.

You can use TryParse which is the same as Parse but it doesn't throw any exception :)

string sample = "false";
Boolean myBool;

if (Boolean.TryParse(sample , out myBool))
{
// Do Something
}

Please note that you cannot convert any type of string to type Boolean because the value of a Boolean can only be True or False

Hope you understand :)

Bash boolean expression and its value assignment

You could do:

[ "$PROCEED" = "y" ] ; BOOL=$?

If you're working with set -e, you can use instead:

[ "$PROCEED" = "y" ] && BOOL=0 || BOOL=1

BOOL set to zero when there is a match, to act like typical Unix return codes. Looks a bit weird.

This will not throw errors, and you're sure $BOOL will be either 0 or 1 afterwards, whatever it contained before.

Can I assign test result to a boolean variable in Java?

Yes. A comparison produces a boolean value, and it can be assigned to a variable just as any other value.

The second form (with the ternary ?: operator) is redundant and should not be used.

Stylistically, I normally enclose boolean expressions in parentheses when assigning them to values, as

boolean bool = (aString.indexOf(subString) != -1);

in order to make a strong visual distinction between the two operators using the = symbol, but this is not required.

Converting a string into a boolean condition

you don't.

it already is a boolean expression.

something.contains("value") -> this returns either true or false
&&
somethingElse.equals("one"); -> this also returns true or false.

what you need, is either:

boolean strCondition = something.contains("value") && somethingElse.equals("one");
if ( strCondition )

or

if ( something.contains("value") && somethingElse.equals("one"))

EDIT:

The above would either return true, false, or throw a nasty NullPointerException.

To avoid the latter, you should use:

if ( "one".equals(somethingElse) && (something != null && something.contains("value"))

How can I declare and use Boolean variables in a shell script?

Revised Answer (Feb 12, 2014)

the_world_is_flat=true
# ...do something interesting...
if [ "$the_world_is_flat" = true ] ; then
echo 'Be careful not to fall off!'
fi

Original Answer

Caveats: https://stackoverflow.com/a/21210966/89391

the_world_is_flat=true
# ...do something interesting...
if $the_world_is_flat ; then
echo 'Be careful not to fall off!'
fi

From: Using boolean variables in Bash

The reason the original answer is included here is because the comments before the revision on Feb 12, 2014 pertain only to the original answer, and many of the comments are wrong when associated with the revised answer. For example, Dennis Williamson's comment about bash builtin true on Jun 2, 2010 only applies to the original answer, not the revised.

Proper syntax to set variable to a boolean with two string comparisons in bash

[[ produces no output, so you can't assign to a variable as you have tried. Even if it did produce output, then your syntax would be incorrect, as the way to assign the output of a command to a variable is variable=$(command).

The way that [[ works is by returning success when the conditions evaluate to true, so you could use a makeshift boolean if you wanted, by changing the first line to something like this:

[[ "${os}" == "ios" && "${version}" == "public" ]] && isIosPublicVersion=1
if [[ -n $upload ]] && [[ $isIosPublicVersion -ne 1 ]]

Alternatively, you could use a function:

isIosPublicVersion() { [[ "${os}" == "ios" && "${version}" == "public" ]]; }
if [[ -n $upload ]] && !isIosPublicVersion

The return code of a function is equal to the return code of the last command that was evaluated.



Related Topics



Leave a reply



Submit