I would like to identify the string "mystring" in a text (actually a R code), but ensuring that it is a variable name:
it should not be part of a longer variable such as "thisismystring" or "mystringisnice". I guess this can be done by excluding (?!([[:alnum:]]){1}) before and after "mystring"?
It can however be at the beginning of a line (such as "\nmystring") so we have to be careful about this type of exception
What is the best approach for that?
Edit: unit test
pattern = "\\bmystring\\b"
identical(grepl(pattern = pattern,
x = c("thisismystring","mystringisnice","\nmystring", "mystring", "mystring=", "mystring(", " mystring","mystring\n", "6mystring", "mystring0", "= mystring(", "=mystring",
"hop
mystring")),
c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE))
Just use the \b (word boundary) token:
\\bmystring\\b
unit test
pattern = "\\bmystring\\b"
`==`(grepl(pattern = pattern,
x = c("thisismystring","mystringisnice","\nmystring", "mystring", "mystring=", "mystring(", " mystring","mystring\n", "6mystring", "mystring0", "= mystring(", "=mystring",
"hop
mystring")),
c(FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE))
# [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Related
defmodule My do
def go do
x = nil
if(not x) do
IO.puts "hello"
else
IO.puts "goodbye"
end
end
end
In iex:
/elixir_programs$ iex c.exs
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> My.go
** (ArgumentError) argument error
c.exs:5: My.go/0
iex(1)>
According to Programming Elixir >= 1.6, p.35:
Elixir has three special values related to Boolean operations: true,
false, and nil. nil is treated as false in Boolean contexts.
It doesn't seem to be true:
defmodule My do
def go do
x = false
if (not x) do
IO.puts "hello"
else
IO.puts "goodbye"
end
end
end
In iex:
~/elixir_programs$ iex c.exs
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> My.go
hello
:ok
iex(2)>
#spec not true :: false
#spec not false :: true
def not value do
:erlang.not(value)
end
Elixir 's latest definition of not function shows that it only receive false and true.
However, nil is not belongs to them, so it shows argument error.
Elixir has three special values related to Boolean operations: true, false, and nil. nil is treated as false in Boolean contexts.
nil is just an atom, that is nil === :nil.
You can consider using ! operator, which in fact is Kernel.! macro.
Receives any argument (not just booleans) and returns true if the
argument is false or nil; returns false otherwise.
!nil will return true.
"Kernel.not/1" or not/1 expects a Boolean value
note: Other values different of nil and false are true
try this example:
x = nil
if (x) do true else false end
false
Examples with short-if condition and true, false, nil values
iex> if nil , do: true, else: false
false
iex> if !nil , do: true, else: false
true
iex> if false , do: true, else: false
false
iex> if !false , do: true, else: false
true
iex> if not false , do: true, else: false
true
iex> if true , do: true, else: false
true
iex> if !true , do: true, else: false
false
iex> if not true , do: true, else: false
false
So currently I use next style:
{
BasedOnStyle: "LLVM",
IndentWidth: 4,
UseTab: false,
ColumnLimit: 150,
Standard: "Cpp11",
BreakBeforeBraces: "Attach",
BreakBeforeBinaryOperators: false,
AlwaysBreakTemplateDeclarations: true,
AllowShortLoopsOnASingleLine: false,
AllowShortIfStatementsOnASingleLine: false,
AllowAllParametersOfDeclarationOnNextLine: true,
SpacesInParentheses: true,
SpacesBeforeTrailingComments: 1,
SpaceInEmptyParentheses: false,
SpaceAfterControlStatementKeyword: true,
PointerBindsToType: true,
MaxEmptyLinesToKeep: 1,
IndentFunctionDeclarationAfterType: true,
IndentCaseLabels: true,
ExperimentalAutoDetectBinPacking: true,
DerivePointerBinding: true,
Cpp11BracedListStyle: false,
ConstructorInitializerAllOnOneLineOrOnePerLine: true,
BreakConstructorInitializersBeforeComma: true
}
and get
try {
}
catch ( ... ) {
}
While I want to get
try {
} catch ( ... ) {
}
Can any one say which Clang-Format Style Option is responsible for such behaviour?
BreakBeforeBraces should, as I understand it, affect the behavior you are concerned with. Attach looks like the correct option from it description on the style options page that you referenced. The only reason that I can see for it not working is that BreakBeforeBraces expects a BraceBreakingStyle enum. Try it without the making Attach a string.
BreakBeforeBraces: Attach
or
BreakBeforeBraces: BS_Attach
Support for try-catch-blocks has only been added recently. If you update to a current version, this should be fixed.
I want to set a variable in Python to true or false. But the words true and false are interpreted as undefined variables:
#!/usr/bin/python
a = true;
b = true;
if a == b:
print("same");
The error I get:
a = true
NameError: global name 'true' is not defined
What is the python syntax to set a variable true or false?
Python 2.7.3
First to answer your question, you set a variable to true or false by assigning True or False to it:
myFirstVar = True
myOtherVar = False
If you have a condition that is basically like this though:
if <condition>:
var = True
else:
var = False
then it is much easier to simply assign the result of the condition directly:
var = <condition>
In your case:
match_var = a == b
match_var = a==b
that should more than suffice
you cant use a - in a variable name as it thinks that is match (minus) var
match=1
var=2
print match-var #prints -1
Python boolean keywords are True and False, notice the capital letters. So like this:
a = True;
b = True;
match_var = True if a == b else False
print match_var;
When compiled and run, this prints:
True
you have to use capital True and False not true and false
as Poke said:
If you have a condition that is basically like this though:
if <condition>:
var = True
else:
var = False
then it is much easier to simply assign the result of the condition
directly:
var = <condition>
but if you want to reverse it you can use:
var = <condition> is False
I'm trying to construct a 2D array in the form of pointers-to-pointers. This doesn't work:
bool** data = {
new bool[4] {true, true, true, true},
new bool[4] {true, false, false, true},
new bool[4] {true, false, false, true},
new bool[4] {true, true, true, true}
};
Is it possible? How should I be doing it?
EDIT:
Looks like I might be trying to do the wrong thing. I have a function that takes a 2D array of bools of an unknown size, along with integer width and height, as arguments. At present, the signature is:
foo(bool** data, int width, int height)
I want to be able to construct a literal for data, but I also need this function to work for any size of array.
You could have an array of arrays (sometimes referred to as a multi-dimensional array):
bool data[][4] = {
{true, true, true, true},
{true, false, false, true},
{true, false, false, true},
{true, true, true, true}
};
However, that isn't convertible to bool**, so if you need that conversion then this won't work.
Alternatively, an array of pointers to static arrays (which is convertible to bool**):
bool data0 = {true, true, true, true};
bool data1 = {true, false, false, true};
bool data2 = {true, false, false, true};
bool data3 = {true, true, true, true};
bool * data[] = {data0, data1, data2, data3};
or if you really want dynamic arrays (which is almost certainly a bad idea):
bool * make_array(bool a, bool b, bool c, bool d) {
bool * array = new bool[4];
array[0] = a;
array[1] = b;
array[2] = c;
array[3] = d;
return array;
}
bool * data[] = {
make_array(true, true, true, true),
make_array(true, false, false, true),
make_array(true, false, false, true),
make_array(true, true, true, true)
};
or, perhaps, you could stick with arrays, and modify your functions to take references to arrays rather than pointers, inferring the dimensions as template parameters if you need to support different dimensions. This is only possible if the dimensions are always known at compile time.
template <size_t N, size_t M>
void do_something(bool (&array)[N][M]);
do_something(data);
bool data_[][4] = {
{true, true, true, true},
{true, false, false, true},
{true, false, false, true},
{true, true, true, true}
};
bool *data[4] = { data_[0], data_[1], data_[2], data_[3] };
bool *data[] = {
(bool []){true, true, true, true}
,(bool[]) {true, false, false, true}
,(bool[]) {true, false, false, true}
,(bool[]) {true, true, true, true}
};
This is convertible to bool** as you wish. This is not an array of arrays. This is an array of pointers.
By prefacing each row with (bool []) we enable it to decay to a pointer. Each row is therefore a pointer, and can then be brought together into an array of four pointers.
Edit: You can drop the explicit 4 inside the [braces], as it can be deduced by the compiler.
First of all, sorry for the confused title.
What I want to do is to convert {1, 4, 9} to:
{True, False, False, True, False, False, False, False, True}
That is, only the indexes from the first list will have value True, the rest will be False.
I sense there is some really simple solution, but I am quite new to both Mathematica and functional programming. I could do it iteratively, in a loop, but there has to be something that works with the list as a whole. Right? :)
Thanks for your help.
EDIT: to show that I tried to do something before I asked, here's my progress so far:
first={1,4,9}
ReplacePart[Table[False, {x, Max[first]}], {1} -> True]
(* gives {True, False, False, False, False, False, False, False, False} *)
Unfortunately, it doesn't work with {1,4,9} -> True, but would work with {1 -> True, 4 -> True, 9 -> True}. But I don't know how to get to that...
EDIT 2: got it.
ReplacePart[Table[False, {x, Max[first]}], Table[x -> True, {x, first}]]
I'd still love to see your solutions! This one seems like an ugly hack to me ... :)
Here's a simple approach:
first = {1, 4, 9};
list = ConstantArray[False, Max#first];
list[[first]] = True;
list
Out[1]= {True, False, False, True, False, False, False, False, True}
Here's the above solution written as a convenient function:
Clear[convertIndices]
convertIndices[index_List] :=
Module[{list = ConstantArray[False, Max#index]},
list[[index]] = True; list]
Usage:
convertIndices#{1, 4, 9}
Out[2]= {True, False, False, True, False, False, False, False, True}
I would use SparseArray for this operation. In my opinion it is very easy to understand, and it is also efficient, especially when a low percentage of indices are True.
true = {1, 4, 9};
SparseArray[(List /# true) -> True, Automatic, False]
Alternatively with Transpose (which looks better when pasted into Mathematica):
SparseArray[{true}\[Transpose] -> True, Automatic, False]
You can use Normal if you must convert the output to a normal array, but most operations will not require that.
Also, sacrificing practicality for terseness:
#==1 & /# SparseArray[List /# true -> 1]
Actually, I would have used Yoda's answer myself, but here's an alternative:
first = {1, 4, 9};
MemberQ[first, #] & /# Range[Max[first]]
(* ===> {True, False, False, True, False, False, False, False, True}*)
Or this one:
Or ### Outer[Equal, Range[Max[first]], first]
(* ===> {True, False, False, True, False, False, False, False, True}*)
Both have the advantage that they skip Yoda's ConstantArray initialization step.