Unexpected result stored in attribute populated by DXL script - ibm-doors

I'm now the maintainer for a bunch of dxl scripts, and I am in the process of testing them all.
The script in question formats multiple strings into a buffer, then converts back to a string. This string is then concatenated with another string, and then stored in an attribute. The code is below (variable names and number of function parameters have been sanitised FYI):
string formatForField(string s1, string s2){
Buffer buff = create();
buff += s1
buff += "\n";
buff += s2;
string formattedString = tempStringOf(buff);
delete(buff);
return formattedString;
}
void alterField(string inputString1, string inputString2){
string formattedChange = formatForField(inputString1, inputString2);
string oldValue = currentObject."Attribute Name";
if (oldValue != "") {
oldValue = oldValue "\n---\n";
}
string newValue = oldValue formattedChange;
currentObject."Attribute Name" = newValue;
}
The problem is that occasionally instead of the expected result, the current objects's value of "Attribute Name" has □ appended onto the previous value.
If for example, the value of (current object)."Attribute Name is "Lorem Ipsum" and after running the script, the expected value of (current object)."Attribute Name is:
Lorem Ipsum
---
inputString1
inputString2
Then occasionally, the value of (current object)."Attribute Name will actually be:
Lorem Ipsum
---
□
I've not been able to locate any similar reported issues online, so I'm posting here. I'm not sure what is happening here, as 99% of the time the script provides the expected output. Interestingly, if the attribute contains the white square after running the script, running the script again with the same input will result in the expected output...
Edit: This image shows the white square I am on about, as it doesn't seem to show up in the live post view
Extra Edit: Upon further inspection, I've found that sometimes additional random characters are included. The right column is supposed to contain text, not the random characters it does contain.

Forgive me if I state the obvious here, but I'm not really sure where the problem exactly lies. From what I read, alterField has the following characteristica.
Expected input are two strings. Precondition: both of them must have a value.
The function checks whether the attribute "Attribute Name" already has a value. If so, the old value shall be preserved and the two strings shall be added to the old value, separated by a line of dashes. The two strings are concatenated by a carriage return.
So, after starting the following script
Object currentObject = first
currentObject."Attribute Name" = "good morning"
String partOne = "good"
String partTwo = "evening"
alterField (partOne, partTwo)
alterField (partOne, partTwo)
alterField (partOne, partTwo)
the attribute "Attribute Name" of the first object of the Module will have the following content:
good morning
---
good
evening
---
good
evening
---
good
evening
If the precondition is not met and partOne is empty (""), partTwo is "evening", then after three calls of alterField the attribute will look like this:
good morning
---
evening
---
evening
---
evening
The question is: what do you expect to happen?

Realised the error. The buff was being deleted just after calling
tempStringOf and so the memory being referenced was deallocated, causing the weirdness. Silly mistake on my part.

Related

Search string with specific termination DXL

Im trying to perform a search in DXL of a string that ends with specific characteres Im not able to find the way to perform this.
Example, I'm looking for
" A: 23.1.23.2.4"
But if this contains at the end the character "~" the find function does not work
Example Where the skip list contains "A: 12.2.1.4.5~ text text text text"
I just need to know in the object.text contains A: 12.2.1.4.5
string string_text = "A: 12.2.1.4.5"
if(find(skip[i],string_text,string_text)){
modify_attributes(req_text)
}else{
output << "stgring not found : "
}
use a regular expression, like this
void modify_attributes (string fulltext) {print "modifying.."}
string fulltext = "A: 12.2.1.4.5~ text text text text"
Regexp searchme = regexp2 "A: 12.2.1.4.5"
if(searchme (fulltext)){
modify_attributes(fulltext)
}else{
print "string not found "
}
The "find"-method for Skip lists is O(1), if I am not mistaken. But for that to work properly, the key, you are asking for, has to match exactly.
So, to benefit from the speed of value-retrieval by the find method, I suggest, that you have a look at your code part, where you put stuff into your Skip, (only put "clean" information in the Skip, which you know, you want to ask for later on).
That of course only works, if you have the possibility to do so, i.e. you don't get the Skip from somewhere you don't have control over..

Lua String Manipulation (Find Words Before & After)

I'm fairly new to this forum. I am having trouble with manipulating the correct string to achieve this.
Basically, what I'm trying to do is receive an input string like this example:
str = "Say hello to=Stack overflow, Say goodbye to=other resources"
for question, answer in pairs(string.gmatch(s, "(%w+)=(%w+)"))
print(question, answer)
end
I want it to return: question = "Say hello to" and answer = "Stack overflow, question = "Say goodbye to" and so on and so forth. but instead, it picks up the word just before the equal sign and the word just after. I've even tried the * quantifier, and it does the same exact thing.
I've also tried this pattern
[%w%s]*=[%w%s]
I just want to be able to sort this string into a key-value table where the key is all words before each = and the value is all words after that equal but before the comma.
Does anyone have a suggestion?
You can use something like this:
local str = "Say hello to=Stack overflow, Say goodbye to=other resources"
for question, answer in string.gmatch(str..",", "([^=]+)=([^,]+),%s*") do
print(question, answer)
end
"([^=]+)=([^,]+),%s*" means the following: anything except = ([^=]) repeated 1 or more times (+) followed by = and then anything except ',', followed by comma and optional whitespaces (to avoid including them in the next question). I also added comma to the string, so it parses the last pair as well.
To elaborate a bit further per request in the comments: in the expression [^=]+, [=] designates a set with one allowed character (=) and [^=] negates that, so it's a set with any character allowed except = and + allows the set to be repeated 1 or more times.
As #lhf suggested you can use a simpler expression: (.-)=(.-),%s*, which means: take all characters until the first = (- makes matching non-greedy) and then take all characters until the first ,.

Difference between "nil" ,"empty" and "few space bar typed" in Swift

I have a text box, what values will it contain if:
I type nothing.
I type few space bars.
Are these 2 different things? Is nil different from both of them?
nil used when an object or value doesn't exist. text on a UITextField should never be nil as its default value is the empty string ("").
The empty string "" is different from a string with spaces " ". A string with spaces contains characters, and will have a length however many whitespace characters there are in the string. The length of the empty string is 0.
This question must have been asked a lot of times, but a reminder is always welcome for newcomers in iOS development (even if, as marked is the comments, a little experience by yourself could have been useful).
First what is nil ? Well, it's nothing, is that simple. An empty String is different from nil, since it's an object, a String with 0 characters in it. A String with multiple spaces in it is also different from these two values, because the String length is different from 0, even if once displayed, your String looks empty !
FYI, before iOS6, the default value of the text property on an UITextField was nil, and it was necessary to test it before using it. Now the default value is an empty string "". I guess the textis an optional String in Swift because of this historical default value.

from list to string and back to list

I have read a multiline file and converted it to a list with the following code:
Lines = string:tokens(erlang:binary_to_list(Binary), "\n"),
I converted it to a string to do some work on it:
Flat = string:join(Lines, "\r\n"),
I finished working on the string and now I need to convert it back to a multiline list, I tried to repeat the first snippet shown above but that never worked, I tried string:join and that didnt work.. how do i convert it back to a list just like it used to be (although now modified)?
Well that depends on the modifications you made on the flattened string.
string:tokens/2 will always explode a string using the separator you provide. So as long as your transformation preserves a specific string as separator between the individual substrings there should be no problem.
However, if you do something more elaborate and destructive in your transformation then the only way is to iterate on the string manually and construct the individual substrings.
Your first snippet above contains a call to erlang:binary_to_list/1 which first converts a binary to a string (list) which you then split with the call to string:tokens/2 which then join together with string:join/2. The result of doing the tokens then join as you have written it seems to be to convert it from a string containing lines separated by \n into one containing lines separated by \r\n. N.B. that this is a flat list of characters.
Is this what you intended?
What you should do now depends on what you mean by "I need to convert it back to a multiline list". Do you mean everything in a single list of characters (string), or in a nested list of lines where each line is a list of characters (string). I.e. if you ended up with
"here is line 1\r\nhere is line 2\r\nhere is line 3\r\n"
this already is a multiline line list, or do you mean
["here is line 1","here is line 2","here is line 3"]
Note that each "string" is itself a list of characters. What do you intend to do with it afterwards?
You have your terms confused. A string in any language is a sequence of integer values corresponding to a human-readable characters. Whether the representation of the value is a binary or a list does not matter, both are technically strings because of the data they contain.
That being said, you converted a binary string to a list string in your first set of instructions. To convert a list into a binary, you can call erlang:list_to_binary/1, or erlang:iolist_to_binary/1 if your list is not flat. For instance:
BinString = <<"this\nis\na\nstring">>.
ListString = "this\nis\na\nstring" = binary_to_list(BinString).
Words = ["this", "is", "a", "string"] = string:tokens(ListString, "\n").
<<"thisisastring">> = iolist_to_binary(Words).
Rejoined = "this\r\nis\r\na\r\nstring" = string:join(Words, "\r\n").
BinAgain = <<"this\r\nis\r\na\r\nstring">> = list_to_binary(Rejoined).
For your reference, the string module always expects a flat list (e.g., "this is a string", but not ["this", "is", "a", "string"]), except for string:join, which takes a list of flat strings.

Writing to a file in BlackBerry

Hey Folks,I have pasted my code here. Dialog.inform contains a 'long' value which is a player's score. If i want to write it to a file, i need to convert it to int of byte.I am getting junk value written into the file.Can anyone help me out with this? I want to print the content of (elapsed/34)
Dialog.inform("Congrats You scored : "+(elapsed/34));
try
{
FileConnection fc = (FileConnection)Connector.open("file:///SDCard/Rashmi.txt");
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (!fc.exists())
{
fc.create(); // create the file if it doesn't exist
}
OutputStream outStream = fc.openOutputStream();
int lp=safeLongToInt(elapsed/34);
outStream.write("Rashmi".getBytes());
outStream.write(lp);
outStream.close();
fc.close();
Okay, since you're saving the score to a file presumably you want that score to be viewable as a string. When you view that file I'm guessing that you see the word "Rashmi" followed by one junk character. The reason the variable lp is not being printed correctly is because the string value of a number is different from its numerical value. For instance the binary value of 2 is 10 whereas the ASCII value for '2' is 00110010 (or 50 in decimal). When you call outStream.write(lp) this is printing the numerical value of lp rather than its string value. In short, you need to cast the numerical value to a string. Try something like this:
String text = "Rashmi" + (elapsed/34);
outStream.write(text.getBytes());

Resources