How to construct a valid parameter for findNodeKeyByPath()? - sap

To access items in a SAP tree, most methods need a key which identifies the node. To get such a key, you need the function findNodeKeyByPath(). The description says:
Return the node key for the given path (e.g. 2\1\2).
path The node path. STRING.
I need to get the first child of the first item in the tree. Using "0" throws an exception. Indexes of the root element seem to start at 1 instead of 0. Using "1" and "2" gives me correct keys.
But anything I try with a backslash does not work. "1\0" does not throw an exception, neither does "1\1". But both parameters return the key of node "1" and not of a subnode.
How do I need to construct the path in SilkTest (Silk4J, SAP) to get a valid key?

In Java, "\1" is the octal representation of ASCII character 0x01. See What does \1 represent. The string "1\\1" gives a valid path and returns the correct key.
However, the node must be visible, otherwise an empty string is returned. That means, you need the following code:
SapTree tree = ...; // initialize somewhere
String parentKey = tree.findNodeKeyByPath("1");
tree.expandNode(parentKey);
String key = tree.findNodeKeyByPath("1\\1");

Related

string operation in ruby

I have a below string array object which has 4 elements. I want to compare the elements of this list to an string and want to check if string is part of this list.
list = ["starter_v2", "professional_q", "custom_v", "basic_t9"]
str = "starter"
if list.include?str #should return true but returning false as it is checking the full string not the substring
Above if condition should return true, however it is returning false.
Can someone suggest me how to fix this, as I am new to ruby and want to compare strings.
For my usecase, in list object I will always have entries with "_" followed by an alphabetic character and I will compare this by string without "_"
Enumerable#include? checks if a given value matches any value in the given enumerable. A substring is not equivalent to a string that contains it, so this check fails.
Instead, you want to check if any string in the array matches your substring. Ruby has handy facilities for this: Enumerable#any? lets you iterate an enumerable, yielding each element to a block, and then will return true if any invocation of the block returns true.
So, you can use:
list.any? {|element| element.include?(str) }
What this will do is check each entry in list to see if str is included in it; once a match is found, it'll stop iterating and return true. If it goes through the entire list without finding a match, it'll return false.
You could also use use element.start_with? if you know that your search string should always match the first part of the string, or you could use a more complex condition which splits each element on underscore and compares the first part, or you could use a regex. The important part is that the block returns true when you want to indicate a match.

Can't get key of object that is numeric

I'm working with an API that returns an array of objects. I can get all the keys, but two of those have numbers as keys, and I cannot get it. Give me an error.
I really dont know why I can not get it those keys.
Is there something different due to are numbers?
BTW Im using axios.
If you're using dot notation, you should change to bracket notation to access properties start by a number.
The code below uses dot notation, it throws an error
const test = {"1h" : "test value"};
console.log(test.1h); // error
Why :
In the object.property syntax, the property must be a valid JavaScript
identifier.
An identifier is a sequence of characters in the code that identifies a variable, function, or property.
In JavaScript, identifiers are case-sensitive and can contain Unicode letters, $, _, and digits (0-9), but may not start with a digit.
The code below uses bracket notation, works fine
const test = {"1h" : "test value"};
console.log(test["1h"]); // works
Why :
In the object[property_name] syntax, the property_name is just a
string or Symbol. So, it can be any string, including '1foo', '!bar!',
or even ' ' (a space).
Check out the document here

URL-parameters input seems inconsistent

I have review multiple instructions on URL-parameters which all suggest 2 approaches:
Parameters can follow / forward slashes or be specified by parameter name and then by parameter value. so either:
1) http://numbersapi.com/42
or
2) http://numbersapi.com/random?min=10&max=20
For the 2nd one, I provide parameter name and then parameter value by using the ?. I also provide multiple parameters using ampersand.
Now I have see the request below which works fine but does not fit into the rules above:
http://numbersapi.com/42?json
I understand that the requests sets 42 as a parameter but why is the ? not followed by the parameter name and just by the value. Also the ? seems to be used as an ampersand???
From Wikipedia:
Every HTTP URL conforms to the syntax of a generic URI. The URI generic syntax consists of a hierarchical sequence of five components:
URI = scheme:[//authority]path[?query][#fragment]
where the authority component divides into three subcomponents:
authority = [userinfo#]host[:port]
This is represented in a syntax diagram as:
As you can see, the ? ends the path part of the URL and starts the query part.
The query part is usually a &-separated string of name=value pairs, but it doesn't have to be, so json is a valid value for the query part.
Or, as the Wikipedia articles says it:
An optional query component preceded by a question mark (?), containing a query string of non-hierarchical data. Its syntax is not well defined, but by convention is most often a sequence of attribute–value pairs separated by a delimiter.
It is also fairly common for request processors to treat a name=value pair that is missing the = sign, as if the it was name=.
E.g. if you're writing Servlet code and call servletRequest.getParameter("json"), it would return an empty string ("") for that last URL in the question.

DBMS_SQL.LAST_ERROR_POSITION: error position for PostgreSQL database

What is the equivalent for DBMS_SQL.LAST_ERROR_POSITION in PostgreSQL to get the offset of an error?
You don't specify in what programming language you want to access this information, and not all APIs give you access to the location of the error, but it is sent with the PostgreSQL error message.
See the documentation for the C API:
PQresultErrorField
Returns an individual field of an error report.
char *PQresultErrorField(const PGresult *res, int fieldcode);
fieldcode is an error field identifier; see the symbols listed below. NULL is returned if the PGresult is not an error or warning result, or does not include the specified field. Field values will normally not include a trailing newline. The caller should not free the result directly. It will be freed when the associated PGresult handle is passed to PQclear.
The following field codes are available:
[...]
PG_DIAG_STATEMENT_POSITION
A string containing a decimal integer indicating an error cursor position as an index into the original statement string. The first character has index 1, and positions are measured in characters not bytes.
PG_DIAG_INTERNAL_POSITION
This is defined the same as the PG_DIAG_STATEMENT_POSITION field, but it is used when the cursor position refers to an internally generated command rather than the one submitted by the client. The PG_DIAG_INTERNAL_QUERY field will always appear when this field appears.

Access numeric NSDictionary key in NSPredicate

I'm trying to write a predicate to check a specific value in a dictionary I have set up.
This dictionary has string versions of "0", "1", and "2" as keys which I would like to access.
The predicate I would like to write is:
$player.currencyDictionaries.0.earned > 1000
The problem is that .0 is not allowed. Assuming I cannot easily change how the dictionary stores values (this is in older versions and I'd like to use the same predicate on all versions as it is hosted on a server) is there any way to access the data?
IIRC, you can do this:
$player.currencyDictionaries[0].earned > 1000
(You might need to do ['0'] to guarantee that the passed value is a string and not a number)
Note that this syntax ([0]) only works in predicate format strings. It does not work with key paths.
This syntax is defined under the "Index Expression" section of the Predicate BNF.
EDIT
Actually, this won't work, and here's why:
The string "$player.currencyDictionaries[0].earned" will be read and turned into an NSExpression of type NSKeyPathExpression. This means, when it's evaluated, it's going to basically take that string and run it through the receiver's -valueForKeyPath: method. As I mentioned above, the bracket syntax doesn't work with key paths, and thus this will produce the incorrect answer.
However, since you know the currencyDictionaries returns an NSDictionary, and since NSDictionary overrides the -valueForKey: method, you can turn the [0] bit into a key path, by turning it into a literal string:
$player.currencyDictionaries.'0'.earned