ASTParser - Replace for(;;) with for(;true;) - abstract-syntax-tree

I have an AST like
for(;;){/*...*/}
It means when I call
forStatement.getExpression()
I get null. But how to replace null with BooleanLiteral (true)? Thanks

Related

Kamailio - get first occurrence value of a param in a string

I need to get the value of first tgrp param in this string using Kamailio :
$var(x) = <sip:xxxxxxxxx;tgrp=0001000;trunk-context=xx.xx.xx.xx#xx.xx.xx.xx:5060;transport=UDP;user=phone;tgrp=237>
I’m trying $var(y) = $(var(x){param.value,tgrp}); but it’s getting the last value of tgrp which is 237>.
Noting that first tgrp is not always in the second index , some other parameters can be added to the string.
How to get the value of first occurrence of tgrp param ?
param.value string operations designed to work with unique params names.
You can loop over all params using for loop and checking {param.name,index}
Try a solution based on xavp_params_explode():
https://kamailio.org/docs/modules/stable/modules/pv.html#pv.f.xavp_params_explode
Something like:
xavp_params_explode("$(var(x){s.unbracket})", "x");
xdbg("$xavp(x=>tgrp[0])"); # <- print the value of first parameter tgrp
The index [0] can be omitted, without it first value being returned, but if you want the second param value, then use [1] as index.

Select any character string over an NA in an If statement in R

I am trying to create a function which will look at two vectors of character labels, and print the appropriate label based on an If statement. I am running into an issue when one of the vectors is populated by NA.
I'll truncate my function:
eventTypepriority=function(a,b) {
if(is.na(a)) {print(b)}
if(is.na(b)) {print(a)}
if(a=="BW"& b=="BW",) {print("BW")}
if(a=="?BW"& b=="BW") {print("?BW")}
...#and so on
}
Some data:
a=c("Pm", "BW", "?BW")
b=c("PmDP","?BW",NA)
c=mapply(eventTypepriority, a,b, USE.NAMES = TRUE)
The function works fine for the first two, selecting the label I've designated in my if statements. However, when it gets to the third pair I receive this error:
Error in if (a == "?BW" & b == "BW") { :
missing value where TRUE/FALSE needed
I'm guessing this is because at that place, b=NA, and this is the first if statement, outside of the 'is.na' statements, that need it to ignore missing values.
Is there a way to handle this? I'd really rather not add conditional statements for every label and NA. I've also tried:
-is.null (same error message)
-Regular Expressions:
if(a==grepl([:print:]) & b==NA) {print(a)}
In various formats, including if(a==grepl(:print:)... No avail. I receive an 'Error: unexpected '[' or whatever character R didn't like first to tell me this is wrong.
All comments and thoughts would be appreciated. ^_^
if all your if conditions are exclusives, just call return() to avoid checking other conditions when one is met:
eventTypepriority=function(a,b) {
if(is.na(a)) {print(b);return()}
if(is.na(b)) {print(a);return()}
if(a=="BW"& b=="BW",) {print("BW");return()}
if(a=="?BW"& b=="BW") {print("?BW");return()}
...#and so on
}
You need to use if .. else statements instead of simply if; otherwise, your function will evaluate the 3rd and 4th lines even when one of the values is n/a.
Given you mapply statement, I also assume you want the function to output the corresponding label, not just print it?
In that case
eventTypepriority<-function(a,b) {
if(is.na(a)) b
else if(is.na(b)) a
else if(a=="BW"& b=="BW") "BW"
else if(a=="?BW"& b=="BW") "?BW"
else "..."
}
a=c("Pm", "BW", "?BW")
b=c("PmDP","?BW",NA)
c=mapply(eventTypepriority, a,b, USE.NAMES = T)
c
returns
Pm BW ?BW
"..." "..." "?BW"
If you actually want to just print the label and have your function return something else, you should be able to figure it out from here.

Handle Null condition

I want to handle null condition in below code.
lstTest.Discount = If((Not dataSet.Tables("History") Is Nothing),
If(IsDBNull(dataSet.Tables("History").Rows(0)("DiscountsAdjustmentsAmount")),
"$0.00",
StringToCurrency(GetContractualDiscount(dataSet.Tables("History").Rows(0)
("DiscountsAdjustmentsAmount"), dataSet.Tables("History").Rows(0)
("DiscountsAdjustments"), dataSet.Tables("History").Rows(0)
("EstimatedCharges")))), "$0.00")
My code is getting break at
dataSet.Tables("History").Rows(0)("DiscountsAdjustments")
since its value is null. I want to replace null value with "0.00"
Please help how can I handle.
Thanks
Rahul,
You will likely need to rewrite this part of it. Here is your original code:
lstTest.Discount = If((Not dataSet.Tables("History") Is Nothing),
If(IsDBNull(dataSet.Tables("History").Rows(0)("DiscountsAdjustmentsAmount")),
"$0.00",
StringToCurrency(GetContractualDiscount(dataSet.Tables("History").Rows(0)
("DiscountsAdjustmentsAmount"), dataSet.Tables("History").Rows(0)
("DiscountsAdjustments"), dataSet.Tables("History").Rows(0)
("EstimatedCharges")))), "$0.00")
Instead of this big nested mess... why not do it this way. Note I dont have a VB debugger in front of me so there may be some slight format adjustments, so consider this pseudo code:
Is the dataset valid
If Not IsDBNull(dataSet.Tables("History"))
''We know that we have data in our dataset
''Do all your checks
if Not isDBNull(dataSet.Tables("History").Rows(0)("Your field"))
''Do something
Else
''Show a 0
END IF
''REPEAT THE ABOVE LINES FOR EACH FIELD
End if
You can check for null on any column first using methods on the DataRow object:
Which of IsDBNull and IsNull should be used?

How to use AND in a CASE sqlite statement for validation check?

I'v a SQLite database and I want to generate a kind of validation check.
The main target is to compare three fields in a table. If stnd_sht has the value 'nicht gegeben' then there should be a entry either in mass_sof of in mass_6m, if not then plausibility is not given.
So far I tried the following:
SELECT
b.baum_id AS baum_id,
CASE
WHEN b.stnd_sht ='nicht gegeben' AND b.mass_sof IS NULL THEN 'fehler'
WHEN b.stnd_sht ='nicht gegeben' AND b.mass_6m IS NULL THEN 'fehler'
ELSE 'plausibel' END AS plaus
FROM baeume b;
and...
SELECT
b.baum_id AS baum_id,
CASE WHEN (b.mass_sof IS NULL OR b.mass_6m IS NULL) AND b.stnd_sht ='nicht gegeben' THEN 'fehler'
ELSE 'plausibel' END AS plaus
FROM baeume b;
Everything works fine without any AND or OR operator but as I add additional expressions the result is not correct.
Is the AND operator not supportet by the CASE statement, or am I totaly wrong and the statement needs another structure or has to be more complex?
Thanks in advance, Patrick
These expressions are syntactically correct, but do not correspond to the explanation.
Sometimes, it helps to reverse a logical condition.
According to the explanation, there is an error if stnd_sht is nicht gegeben and if both the mass_sof and mass_6m values are NULL.
This would correspond to the following expression:
SELECT baum_id,
CASE WHEN stnd_sht = 'nicht gegeben' AND
mass_sof IS NULL AND
mass_6m IS NULL
THEN 'fehler'
ELSE 'plausibel'
END AS plaus
FROM baeume;
This could be simplified a little:
SELECT baum_id,
CASE WHEN stnd_sht = 'nicht gegeben' AND
COALESCE(mass_sof, mass_6m) IS NULL
THEN 'fehler'
ELSE 'plausibel'
END AS plaus
FROM baeume;

How to test QueryString

I have a query string called propID and I wanna check if the passed value in it is a legal integer or not to avoid throwing an error that might reveal info about my database, how can I do it?
In other words, I want something like -but in vb.net- :
IF QueryString("propID").Content.Type = "int32" Then Proceed
You could use TryParse:
Dim myInt As Integer
If Int32.TryParse(QueryString("propID").Content, myInt) Then Proceed
Dim result as boolean
result = integer.tryparse(QueryString("propID"), myintegervariable)
boolean will return true if it parsed correctly (putting the value into your myintegervariable) and will return false if the parsing failed.
You can also write is as
if integer.tryparse(QueryString("propID"), myintegervariable) then
//continue going along with myintegervariable
else
//parsing didn't work
end if
You can just use Int32.TryParse.
You could try the 'is' keyword to check the type of on object.
If QueryString("propID").Content.Type Is Int32 Then Proceed
Otherwise Int32.TryParse would work as well.
C# version:
int _PropID;
if (int.TryParse(QueryString["propID"], out _PropID))
{
//Proceed with _PropID
}

Resources