Efficient extraction of several numbers from a character string in R [duplicate] - regex

This question already has answers here:
R: convert list of numbers from character to numeric
(3 answers)
Closed 7 years ago.
Suppose I have the character string
x <- " 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136"
and I want to extract the floats to end up with this vector as output:
c(1.1325, -0.9022, -0.1832, -0.5479, 0.1236, -0.6556, -1.0599, -0.8881, -0.2136)
What I managed to achieve is:
na.omit(as.numeric(strsplit(samp, split = " ")[[1]]))
My question: Is there a more efficient way?

We can use scan
scan(text=x, what=numeric(), quiet=TRUE)
#[1] 1.1325 -0.9022 -0.1832 -0.5479 0.1236 -0.6556 -1.0599 -0.8881 -0.2136

Related

Strange behaviour while adding string to another string in C++ [duplicate]

This question already has answers here:
How can I repeat a string a variable number of times in C++?
(10 answers)
Closed 2 years ago.
one is 2, and ans is "000000".
string ans = "000000";
ans += string("1", one);
cout<<ans<<endl;
The output is:
0000001�
But I want the output:
00000011
What am I doing wrong?
string("1", one) does not do what you think it does. It does not duplicate the "1" string one number of times. It instead copies the 1st one number of chars from "1", which in this case is the '1' character and the '\0' null-terminator that follows it, which is where the � is coming from in the output. That is not what you want.
Use string(one, '1') instead. That will duplicate the '1' character one number of times, like you want, eg:
ans = "000000";
ans += string(one, '1');
cout << ans << endl;
Just use c++ strings and use + operator to catenate strings.

What is the internal functioning of the following code? [duplicate]

This question already has answers here:
What does i = (i, ++i, 1) + 1; do?
(7 answers)
What does a comma separated list of values, enclosed in parenthesis mean in C? a = (1, 2, 3); [duplicate]
(6 answers)
How does the Comma Operator work
(9 answers)
Closed 3 years ago.
I am trying to understand how the following lines of code work in c++.
int main(){
int i;
i = 1 + (2,3,5,3,6);
cout<<i<<endl;
return 0;
}
Output: 7
Basically, the answer is the sum of 1 and the last integer in between the parentheses.
(2,3,5,3,6) turns out to be 6.
Hence 1 + 6 = 7
You can verify with a print statement
printf("\n%d\n", (2,3,5,3,6));
It will print 6 only.

How to limit input length in JFX textField? [duplicate]

This question already has answers here:
JavaFX TextField Array max length of text value
(1 answer)
How to restrict TextField so that it can contain only one '.' character? JavaFX
(3 answers)
Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number
(1 answer)
Numeric TextField for Integers in JavaFX 8 with TextFormatter and/or UnaryOperator
(4 answers)
Closed 5 years ago.
I need to limit input length to 4 digits in JFX textField (field can be empty or have value 0-9999). Below solution works only partially - I can input only digits , but as many as i want - limit do not work. Even if I remove {0,4} from regex and
change IF condition to:
if(newText.matches("\d") && newText.length()>=0 && newText.length()<5)
it doesn't work too. Where is the error?
JFXtextField.textProperty().addListener((obs, oldText, newText) ->
{
if(newText.matches("\\d{0,4}"))
{
newText = newText;
}
else
{
newText = oldText;
}
});

Non-Greedy Regex Match [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Find shortest matches between two strings
(4 answers)
Closed 5 years ago.
I'd like to match if x > 64 then return 0, 0 end inside this string.
function if x > 64 then return 0, 0 end return 1, 0 end
I'm using if(.*)then(.*)end. However, this matches: if x > 64 then return 0, 0 end return 1, 0 end, which is one end too many.
This should do:
if(.*)then(.*?)end
You could add word boundaries to make sure you match end as a word, not as a part of a variable name:
if(.*)then(.*?)\bend\b
To analyse code, you probably should use a parser instead of a regex, though.

c++: Split string by delimiter [duplicate]

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
How do I tokenize a string in C++?
(37 answers)
Closed 9 years ago.
For example I have this string: hello:there:world
How do I split these three words to string array,
I mean the final result will be this:
arr[0] = "hello";
arr[1] = "there";
arr[2] = "world";
Thanks in advance.