Array of multiple strings possible? Or just array of characters for one string? XGetWindowProperty on atom's where we know a string is returned - x11

When I do XGetWindowProperty on atoms that return strings, such as _NET_WM_NAME, WM_CLASS, WM_NAME. They are returning an array of characters. However when I ask for long or short types like _NET_WM_PID it returns an array of longs, for the PID its just 1 element array, the nitems_return argument is populated with number of elements returned in the array, so for PID its 1. But for the string atoms, the nitems_return hold number of characters returned.
I tested convenience function of XGetWMName and this set the nitems field of XTextProperty to number of characters returned which matches XGetWindowProperty, so I was wondering, is there ever a chance that XGetWindowProperty can returned number of elements in array like for long and short? OR is there only ever just one string associated with those string atoms?
docs on XGetWindowProperty
Thanks

Related

What is [0] and [1..-1] in Ruby?

What do [0] and [1..-1] mean in the following code?
def capitalize(string)
puts "#{string[0].upcase}#{string[1..-1]}"
end
string[0] is a new string that contains the first character of string.
It is, in fact, syntactic sugar for string.[](0), i.e. calling the method String#[] on the String object stored in the variable string with argument 0.
The String#[] method also accepts a Range as argument, to extract a substring. In this case, the lower bound of range is the index where the substring starts and the upper bound is the index where the substring ends. Positive values count the characters from the beginning of the string (starting with 0), negative values count the characters from the end of the string (-1 denotes the last character).
The call string[1..-1] (string.[](1..-1)) returns a new string that is initialized with the substring of string that starts with the second character of string (1) and ends with its last character.
Put together, string[0].upcase is the uppercase version of the first character of string, string[1..-1] is the rest of string (everything but the first character).
Read more about different ways to access individual characters and substrings in strings using String#[] method.

Ruby regex count matched elements in the array of digits

I have a string:
'my_array1: ["1445","374","1449","378"], my_array2: ["1445","374", "1449","378"]'
I need to match all sets of digits from my_array2: [...] and count how many of them there.
I need to do something like this with regex and ruby MatchData
string = 'my_array1: ["1445","374", "1449","378"], my_array2: ["1445","374", "1449","378"]'
matches = string.match(/my_array2\:\s[\[,]\"(\d+)\"/)
count_matches = matches.size
Expected result should be 4.
What is the correct way of doing it?
If you are guaranteed that the content of my_array2 is always numeric you could simply use split twice. First you splitby my_array2: [" and then split by ,. This should give you the amount of items you are after.
If you are not guaranteed that, you could still split by my_array2 and instead of splitting again, you use a pattern such as "\d+" (or "\d+(\.\d+)? if you have floating point values) and count.
An example of the expression is available here.

Understanding BSON Notation

I was trying to understand BSON Notations from the site BSON Site. However I was unable to understand the reason behind the correlations.
I also referred the following questions, but am not convinced because of the following reasons.
Question 1 : Not familiar with ruby implementations
Question 2 : I understood the byte allocation. But unsure about the notations.
I would like to know how the bson object is formed for the following examples in BSON Site
1.{"hello": "world"}
2.{"BSON": ["awesome", 5.05, 1986]}
{"hello": "world"}
\x16\x00\x00\x00
\x02 hello\x00 \x06\x00\x00\x00 world\x00
\x00
(overall: 22 bytes)
The first four bytes contain the overall length as a 32-bit little-endian integer.
\x16\x00\x00\x00 => That's 22 in decimal.
Now comes the first element. The first byte gives the type of data.
\x02 => That's a UTF-8 string.
Then comes the name of the first element, as a null-terminated string.
hello\x00
Next comes the data of the element in the previously given type, in this case a string.
For scannability (so you can quickly skip when you don't need them), strings start with their length, and are null-terminated.
\x06\x00\x00\x00 => That's length 6.
world\x00
Now would come subsequent elements, if there were any. The whole thing is terminated with a null byte.
\x00
{"BSON": ["awesome", 5.05, 1986]}
\x31\x00\x00\x00
\x04 BSON\x00 \x26\x00\x00\x00
\x02 0\x00 \x08\x00\x00\x00 awesome\x00
\x01 1\x00 \x33\x33\x33\x33\x33\x33\x14\x40
\x10 2\x00 \xc2\x07\x00\x00
\x00
\x00
(overall: 49 bytes, array: 38 bytes)
The first four bytes contain the overall length as a 32-bit little-endian integer.
\x31\x00\x00\x00 => That's 49 in decimal.
Now comes the first element. The first byte gives the type of data.
\x04 => That's an array.
Then comes the name of the first element, as a null-terminated string.
BSON\x00
Next comes the data of the element in the previously given type, in this case an array.
[Quote: "The document for an array is a normal BSON document with
integers for the keys, starting with 0 (..)"]
For scannability and because they form document in their own right, arrays start with their length, and are null-terminated.
\x26\x00\x00\x00 => That's 38 in decimal.
Now comes the first element of the array. The first byte gives the type of data.
\x02 => That's a UTF-8 string.
Then comes the name of the first element of the array, null-terminated.
0\x00 => That's key 0.
Next comes the data of the element in the previously given type, in this case a string.
Strings start with their length, and are null-terminated.
\x08\x00\x00\x00 => length 8
awesome\x00
Now comes the second element of the array. The first byte gives the type of data.
\x01 => That's a double floating point number.
Then comes the name of the second element of the array, null-terminated.
1\x00 => That's key 1.
Next comes the data of the element in the previously given type, in this case a double floating point number.
\x33\x33\x33\x33\x33\x33\x14\x40 => That's 5.5.
Now comes the third element of the array. The first byte gives the type of data.
\x10 => That's a 32-bit integer.
Then comes the name of the third element of the array, null-terminated.
2\x00 => That's key 2.
Next comes the data of the element in the previously given type, in this case a 32-bit integer.
\xc2\x07\x00\x00 => That's 1986.
The array is terminated with a null byte.
\x00
The whole thing is terminated with a null byte.
\x00

How to count the number of space-delimited substrings in a string

Dim str as String
str = "30 40 50 60"
I want to count the number of substrings.
Expected Output: 4
(because there are 4 total values: 30, 40, 50, 60)
How can I accomplish this in VB6?
You could try this:
arrStr = Split(str, " ")
strCnt = UBound(arrStr) + 1
msgBox strCnt
Of course, if you've got Option Explicit set (which you should..) then declare the variables above first..
Your request doesn't make any sense. A string is a sequence of text. The fact that that sequence of text contains numbers separated by spaces is quite irrelevant. Your string looks like this:
30 40 50 60
There are not 4 separate values, there is only one value, shown aboveā€”a single string.
You could also view the string as containing 11 individual characters, so it could be argued that the "count" of the string would be 11, but this doesn't get you any further towards your goal.
In order to get the result that you expect, you need to split the string into multiple strings at each space, producing 4 separate strings, each containing a 2-digit numeric value.
Of course, the real question is why you're storing this value in a string in the first place. If they're numeric values, you should store them in an array (for example, an array of Integers). Then you can easily obtain the number of elements in the array using the LBound() and UBound() functions.
I agree with everything Cody stated.
If you really wanted to you could loop through the string character by character and count the number of times you find your delimiter. In your example, it is space delimited, so you would simply count the number of spaces and add 1, but as Cody stated, those are not separate values..
Are you trying to parse text here or what? Regardless, I think what you really need to do is store your data into an array. Make your life easier, not more difficult.

String that can contain multiple numbers - how do I extract the longest number?

I have a string that
contains at least one number
can contain multiple numbers
Some examples are:
https://www.facebook.com/permalink.php?story_fbid=53199604568&id=218700384
https://www.facebook.com/username_13/posts/101505775425651120
https://www.facebook.com/username/posts/101505775425699820
I need a way to extract the longest number from the string. So for the 3 strings above, it would extract
53199604568
101505775425651120
101505775425699820
How can I do this?
#get the lines first
text = <<ENDTEXT
https://www.facebook.com/permalink.php?story_fbid=53199604568&id=218700384
https://www.facebook.com/username_13/posts/101505775425651120
https://www.facebook.com/username/posts/101505775425699820
ENDTEXT
lines = text.split("\n")
#this bit is the actual answer to your question
lines.collect{|line| line.scan(/\d+/).sort_by(&:length).last}
Note that i'm returning the numbers as strings here. You could convert them to numbers with to_i
parse the list (to get an int array), then use the Max function. array.Max for syntax.
s = "https://www.facebook.com/permalink.php?story_fbid=53199604568&id=218700384"
s.scan(/\d+/).max{|a,b| a.length <=> b.length}.to_i

Resources