Groovy script overwriting first byte? - groovy

Hi so this string I'm creating and appending in groovy is somehow corrupting the first byte and I have no idea why this happening. Its the second string creation. In this script I'm making a query and the first one works but the second initialization somehow messes up the first byte in the string and I have to do a substring of an extra index (it's two because I'm initializing a comma). Any insight would be very appreciated!!
Note: I'm using mulesoft runtime 3.8.5 in Anypoint studio 6.4.4. Not sure if this is the reason but it is a candidate in my mind...
flowVars.queryIds = "Id IN ("
for (Integer i = 0; i < payload.size(); i++) {
flowVars.queryIds += "\'" + payload[i].Id + "\',"
}
flowVars.queryIds = flowVars.queryIds.substring(0,flowVars.queryIds.size() - 1) + ")"
//Assigning comma because a random byte is getting inserted and this makes that error explicit & deterministic
flowVars.queryFields = ",";
for (String key : payload[0].keySet()) {
flowVars.queryFields += key + ",";
}
//Skipping over custom field isMatch
flowVars.queryFields = flowVars.queryFields.substring(2, flowVars.queryFields.size() - 9);
return payload

I can't reproduce your problem but since you use groovy, you can write your code a bit shorter:
flowVars.queryIds = "Id IN ("
flowVars.queryIds += payload.collect{"'${it.Id}'"}.join(", ")
flowVars.queryIds += ")"
flowVars.queryFields = payload[0].keySet().join(", ");
this should produce the same output in a more understandable way

So I found out the reason this issue was happening is actually because the csv file I am parsing is corrupted (I thought it was mulesoft and was mistaken). This blog does a greater job explaining the issue than I could. Thanks for your review on the groovy code though Rdmueller! Is definitely a lot cleaner with your suggestions. https://medium.freecodecamp.org/a-quick-tale-about-feff-the-invisible-character-cd25cd4630e7

Related

String with the same content but comparison always returns false

I have the following code, in which using the WiFi library I perform a scan of the available WiFi networks and want to detect if a specific network is available. I am using ESP32 and Arduino IDE. EEPROM memory handling seems to work, but I don't understand why the comparison always returns zero.
SSID = EEPROM.readString(500); // I read from eeprom the string stored in pos 500
WiFi.mode(WIFI_STA);
delay(100);
Serial.println(logg + "SCAN!");
int n = WiFi.scanNetworks();
Serial.println(logg + "SE DETECTARON: " + String(n) + " REDES WIFI!");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.println("'" + WiFi.SSID(i) + "' vs '" + SSID + "' SizeOf: " + String(WiFi.SSID(i).length()) + " - " + String(SSID.length()) + " Comparacion " + String(WiFi.SSID(i) == SSID );
}
delay(10);
What I get on the serial monitor is the following:
'WRT1900AC 2.4GHz' vs 'WRT1900AC 2.4GHz' SizeOf: 16 - 16 Comparacion 0
The two strings look the same, they are the same size. I already tried replacing comparator "==" with strcmp and I get -244.
I also tried using .c_str as follows:
WiFi.SSID(i).c_str() == SSID.c_str()
but with the same results. If someone comes up with something I would be very grateful.
The two strings look the same, they are the same size.
Although the WiFI.SSID() return a String object, however it does not necessary to be ASCII-encoded. The string encoding is depend on the locale setting of the router, and the reason it looks the same is because the Serial.print() cast it into ASCII. This can be proof by the following sketch by using both Serial.print() and Serial.printf() in ESP32 which shown what is actually received (Serial.printf() however does not support Unicode formatting in ESP32 implementation, so it will produce some garbage characters).
int n = WiFi.scanNetworks();
for (int i=0; i<n; i++) {
// Serial.print() will cast the WiFi.SSID() to ASCII
Serial.print(WiFi.SSID(i));
// this shown what WiFi.SSID() truly return
Serial.printf(" --- %s\n", WiFi.SSID(i));
}
This will produce results in show in this picture. As you can see some SSIDs produce the correct results but some shown up as garbage.
So String comparison operator does do the job correctly when you compare WiFi.SSID(i) == SSID and the result indeed is not necessary equal for some SSID, even though it "looks" the same to human.
So how to solve it? If you want to treat them "as the same", ironically, converting String object to char array with .c_str() does do the job because it convert each char to an ASCII. I guess you just didn't use the char array comparison strcmp() correctly.
if(strcmp(WiFi.SSID(i).c_str(), SSID.c_str()) == 0) {
// match
}
else {
// not match
}
If you are saying that this c.str() comparison return -244, then edit your question and do a Serial.printf() on both String or better off to loop through the String character by charter and print out the HEX code to see what's going on.

Remove duplicate string elements C++

The problem consists of finding all permutations using k out of n digits. I'm able to find all the permutations, but I'm struggling trying to erase duplicates. I can successfully compare and find the duplicates, but erasing them is what I'm struggling to do. I have a feeling I'm missing something simple but I don't know what it is.
Any help would be greatly appreciated. I've been staring at this for a week.
Here is the code that I've got right now.
void getPermutations(int n, int k)
{
string str = "";
//fill string with numbers <= n
for(int i = 0; i < n; i++)
{
str += to_string(i); //convert numbers to string
}
string tempStr = "";
string outputStr = "";
do {
tempStr = str.substr(0, k);
int compareResult = tempStr.compare(0, k, outputStr, 0, k);
if (compareResult == 0)
{
cout << "| same | ";
outputStr.erase(k,k);
}
outputStr = tempStr;
cout << outputStr << " ";
} while (next_permutation(str.begin(), str.end()));
}
I think what you meant to do was to erase the contents of tempStr, not outputStr.
The call to erase is not exactly right. Its first argument marks the starting position of your erasing, and the second argument tells how many characters to erase. So if you want to erase the whole string, the first argument should be...
You actually don't have to erase anything. After you get it working your way, try to do it without erasing!
Good Luck!

addition of values in node.js

above is result of below snippet of code
var total_points = 0
for(var i = 0; i < req.body.requisites.length; i++){
total_points = total_points + req.body.requisites[i].points
console.log(req.body.requisites[i].points , total_points)
}
console.log('total points :' + total_points)
req.body.points = total_points
I am not getting why one time it is concatenating the results (see the last values before 'total points') and next time it calculates correctly.
Appreciated if you can help.
Thanks in advance!
As per my earlier comment, it seems like some of your input must be a string instead of number and because of Javascript's coercion rules when adding a string and a number you are getting string concatenation instead of math addition.
You can force all the input to numbers so you always get addition like this:
var total_points = 0
for (var i = 0; i < req.body.requisites.length; i++) {
total_points = total_points + (+req.body.requisites[i].points);
console.log(req.body.requisites[i].points , total_points)
}
console.log('total points :' + total_points)
req.body.points = total_points
And, it might be easier to use .reduce():
req.body.points = req.body.requisites.reduce((total, val) => total + (+val)), 0);
The (+req.body.requisites[i].points) or (+val) converts it to a number if it was a numeric string.
You code seems to be okay.
This might be happening because you are sending the value for one of req.body.requisites[points] as string and that is why it gets concatenated instead on addition.
Check your input or update the question with the input you are passing ie, req.body
Hope this helps you know the reason behind the mess!

how to concatenate a string to a numeric,for eg:i need tkr1,tkr2... based on a for loop

string str1=tkr;
for(i=1;i<=10;i++)
{
string str2=str1+i;
sopln(str2);
}
I need result like this..
tkr1
tkr2
tkr3
tkr4
tkr5...could any one re-write the code which could give the proper output?
Its not mentioned which language you are using so it would be not possible to give exact answer:
This should work in most of the languages :
String str1=tkr;
String str2 = "";
for(i=1;i<=10;i++)
{
str2=str2 + str1+i + " ";
}
print(str2)
And use mutable strings if you are using java

JRI REngine eval() method always returns null

I have a Java servlet running on Tomcat6 which uses JRI (rJava) to communicate with R installed on an Amazon linux server. I installed R from source, installed rJava via cran and set R_HOME and my classpath for Tomcat. I had to setup a few symlinks in one of my java.library.path directories to the necessary native libraries (libjri.so, libR.so and libRblas.so) because I couldn't configure java.library.path to include the directories that they installed in, but I don't think that's a problem. When I run
./run rtest
from the command line in $R_HOME/library/rJava/jri/ I get the expected output and the REngine class seems to be making R statements via eval() fine. But when I call eval() from my class, I get a null instead of REXP object returned in almost all cases. There are no errors logged with or without DEBUG on. The only case where I've gotten a REXP returned is when I set an array, e.g.
array1=c(7245.0, 6003.0, 5504.0)
In this case, I get a REXP whose toString() value is
[REAL* (7245.0, 6003.0, 5504.0)]
But if I then make an eval() call with
summary(array1)
I get back a null instead of the expected REXP. The code, which works on my Mac, is:
private REXP multipleRegression(Datalist list) {
String fitStr = "fit <- lm(";
for (int i = 0; i < list.size(); i++) {
Datastream ds = list.get(i);
String dsStr = ds.getId() + "=" + ds.toArrayString();
System.out.println("VALUE: " + this.eval(dsStr));
System.out.println("SUMMARY: " + this.eval("summary(" + ds.getId() + ")"));
fitStr += ds.getId();
if (i == 0)
fitStr += " ~ ";
else if (i == list.size() - 1)
fitStr += ")";
else
fitStr += " + ";
}
if (list.size() == 1) {
fitStr += "1)";
}
this.eval(fitStr);
return this.eval("summary(fit)");
}
Again, this works on my Mac, so I'm pretty certain the problem is with the environment and not the code. I done several hours of searching and have yet to find a solution. Any help would be wonderful.
Alright, so it did have to do with the native libraries and their symlinks. I got exceptions in my application when I didn't get the symlinks for libjri.so, libR.so and libRblas.so into my java.library.path, but once I got those in, no more exceptions. It turned out I also needed libRlapack.so. Once I symlinked that, I started getting the expected REXP values back.

Resources