String comparison passing through if statement when it shouldn't - grails

I'm comparing values of an excel sheet to record values returned from a database and one record is passing through the if statement when it should fail the if statement.
The if statement looks like this:
if (record.value.equals(cellVal) == false)
{
record.value = cellVal
record.modifyUser = userId
//dataService.updateManualEntry(record)
println "UPDATING ${record.value.equals(cellVal)}"
println "record value: ${record.value}"
updatedCount++
}else{
println "NOT UPDATING [ [ ${record.value.length()} ] + [${cellVal.length()}]"
}
}
The println shows that the value of println "UPDATING ${record.value.equals(cellVal)}" evaluates to be true, in which case I don't understand why it is passing through the if statement. In addition the length of the string is 0.
Can I get a second pair of eyes and figure out why a true value would get through this if statement?

Your printlns happen after you have changed the value to match.

Here:
record.value = cellVal
You have set record.value to cellVal inside the if block. That's why the println returns true.

(true == false) ⇒ false

the println is showing true becoz of the 1st line in your if statement
record.value = cellVal
try printing the same value before if statement.

if ->record.value.equals(cellVal) give u false
u compare (false)==false, naturally false is always = false, hence it will pass through
2nd, look at ur code,
record.value = cellVal
record.modifyUser = userId
//dataService.updateManualEntry(record)
println "UPDATING ${record.value.equals(cellVal)}"
U assign record.value = cellVal before u do a print.
Of cuz it will print true.

Related

How to assign variable an alternative value if first assigned value is not present in groovy?

I have a situation where I would like to get the output of a readJson and assign it to the variable but I would like to check if different key is present in case first one is not present. Here if key "Name" is not present then I would like to check if "address" is present and add it as collection.
followig works for key Name" but I would like to check for "address" in case there is no "Name kry in json object
def originals = readJSON text: sourceStagesText
originalconfign = originals.collectEntries { [(it.Name):it] }.asImmutable()
I tired using || operator but it gives true and false value, instead of true or false how can I assign the value of the command that gives the value to the variable for example
originalconfign = (originals.collectEntries { [(it.Name):it] }.asImmutable() || originals.collectEntries { [(it.address):it] }.asImmutable())
how can I assign the value to originalconfign if it finds Name to name and if it does not then to the address?
This corresponds to your code and just a bit shorter:
originalconfig = originals.collectEntries { [it.Name,it] }.asImmutable() ?: originals.collectEntries { [it.address,it] }.asImmutable()
I think there's an issue in your logic.
The second part will work only if first returns null or empty map.
But second will always return an empty map if first one is empty...
I solved it with if else statment but please let me know if anyone have better short option to acheive this:
if (originals.collectEntries { [(it.Name):it] }.asImmutable()){
originalconfig = originals.collectEntries { [(it.Name):it] }.asImmutable()
} else {
originalconfig = originals.collectEntries { [(it.address):it] }.asImmutable()
}

"for" about table in Lua

tablelogin = {0 = "test1",1 = "test2",2 = "test3",3 = "test4"}
for pp=0,#table do
if takeinternalogin == (tablelogin[pp]) then
LogPrint("Login found")
else
LogPrint("failed login not found")
end
end
takeinternalogin is an internal function of my program that takes the person's login.
In this script I'm taking the person's login and comparing whether or not the login is in the table.
It works, but after the else if the person's login is not in the table, it returns the message "failed login not found" 4 times, that is, it returns the number of times it checks the table.
I don't understand. How can I make the message execute only 1 time?
first of all table is Lua's table library. You should not use it as a variable name. Unless you've added numeric fields to that table or replaced it with something else #table should be 0 and hence your loop should not do anything.
But as you say your loop runs 4 times I suppose you modified table.
You say internallogin is a function so you can never enter the if block as you compare a function value cannot equal a string value: takeinternalogin == (tablelogin[pp] is always false! takeinternallogin would have to return a string.
Why you're using #table here in the first place is unclear to me.
You are currently printing the error message every time it iterates over the table and the current value does not match.
local arr = {[0] = "test1", [1] = "test2", [2] = "test3", [3] = "test4"}
function findLogin(input)
for i,v in pairs(tablelogin) do
if v == input then
LogPrint("Login found")
return i
end
end
LogPrint("failed login not found")
end
login = findLogin(takeinternalogin)
Using return within a loop makes it break out of the loop and in this case never reach the line where it prints the error.

Evaluate stringified json having interpolatable value

Suppose variable b=2 and a stringified json
j = '{"b": "#{b}", "c": null}'
The desired result is:
{
"b" => "2",
"c" => nil
}
My observation:
Since json string contains null, we can not eval it because ruby will say undefined variable or method null. Also, I don't want to replace null with nil.
The only option left is to parse and evaluate.
So I tried the following:
eval(JSON.parse(j).to_s)
which results to
{
"b" => "\#{b}"
}
Please help to achieve desired result?
it should be like this: j = "{'b': '#{b}', 'c': null}"
UPDATE:
Sorry. It should be like this
b = 2
JSON.parse("{ \"b\": \"#{b}\", \"c\": null }")

Why is my MVC ID returning Null (or Nothing)?

My URLs are as follows:
{controller}/{action}/{id}
In this example, it is:
Blog/Edit/2
In my code, I am trying to get the ID parameter (which is "2") like so:
' get route
Dim routeData = httpContext.Request.RequestContext.RouteData
' get id
Dim id = If(String.IsNullOrEmpty(routeData.Values("id") = False), routeData.Values("id").ToString, Nothing)
However, it is saying the value is empty. The following statement returns true for some reason:
If String.IsNullOrEmpty(id) = True Then
How can I get the value of the ID so it isn't NULL (or "Nothing" in VB.NET)?
The solution was to change how I was using the IsNullorEmpty method:
' get id
Dim id = If(String.IsNullOrEmpty(routeData.Values("id")), Nothing, routeData.Values("id").ToString)
' if no id is set, check to see if the user owns the requested entity (company or blog)
If String.IsNullOrEmpty(id) Then
It wasn't returning nothing.
Your original post reads String.IsNullOrEmpty(routeData.Values("id") = False) - your closing bracket is in the wrong place, and so the String.IsNullOrEmpty will always return false. You should instead have written String.IsNullOrEmpty(routeData.Values("id")) = False.
(In VB, "xyz" = false will convert implicitly to "False".)

How would I find the text of a node that has a specific value for an attribute in groovy?

I'm using XMLSlurper. My code is below (but does not work). The problem is that it fails when it hits a node that does not have the attribute "id". How do I account for this?
//Parse XML
def page = new XmlSlurper(false,false).parseText(xml)
//Now save the value of the proper node to a property (this fails)
properties[ "finalValue" ] = page.find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};
I just need to account for nodes without "id" attribute so it doesn't fail. How do I do that?
You could alternatively use the GPath notation, and check if "#id" is empty first.
The following code snippet finds the last element (since the id attribute is "B" and the value is also "bizz", it prints out "bizz" and "B").
def xml = new XmlSlurper().parseText("<foo><bar>bizz</bar><bar id='A'>bazz</bar><bar id='B'>bizz</bar></foo>")
def x = xml.children().find{!it.#id.isEmpty() && it.text()=="bizz"}
println x
println x.#id
Apprently I can get it to work when I simply use depthFirst. So:
properties[ "finalValue" ] = page.depthFirst().find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};

Resources