I am trying to perform a quick fix for a client. We have a report field that shows tolerance values in strings like +/-20 , -19 +18. This is in micrometer and the client wants it in millimeter. So i need to divide only the numeric part of this string by 1000 and display the result.
I am relatively new to crystal reports, with my limited knowledge and from searching this site for suggestions, i created a function with the following code lines,
Function (stringvar x)
local stringvar array input := split(x,"+/-");
val(input[ubound(input)])/1000
The above function works perfectly for tolerance values of +/-. However i am not able to figure out a way to do it for '-19 +18'. I would like to have the result as -0.019 +0.018
I can easily do it in the database source and send it to the report. However the client needs a quick fix of just the report. Any help would be greatly appreciated.
try this
if(Left (x, 3 )="+/-")
then ToNumber(split(x ,"+/-")[2])/100
else if(Left ({x , 1 )="+")
then ToNumber(split(x ,"+")[2])/100
else if(Left (x , 1 )="-")
then ToNumber(split(x ,"-")[2])/100
I figured out an answer which would work for this specific case.
I used the following conditions in a formula field. Based on the condition I called a User defined function 'Tolerance','Tolerance2', 'Tolerance3'.
Formula field:
IF (Left ({PDPRINTDATA.PD_T45}, 3 )="+/-") THEN
'+/-' + ToText(Tolerance({PDPRINTDATA.PD_T45}),3)
ELSE IF (Left ({PDPRINTDATA.PD_T45} , 1 )="-") THEN
'-' + ToText(Tolerance2({PDPRINTDATA.PD_T45}),3) + ' +' + ToText(Tolerance3({PDPRINTDATA.PD_T45}),3)
Tolerance:
Function (stringvar x)
local stringvar array input := split(x,"+/-");
val(input[ubound(input)])/1000;
Tolerance2:
Function (stringvar x) local stringvar mystr := x;
If NumericText (mystr[2 to 3]) Then
ToNumber(mystr[2 to 3])/1000
Else 0
Tolerance3:
Function (stringvar x)
local stringvar mystr := x;
If NumericText (mystr[7 to 8]) Then
ToNumber(mystr[7 to 8])/1000
Else
0
This solution works for me considering the fact that the string will always be of this format '+/-XX' or '-XX +YY'.
Related
I have records coming in to my report that contain fields "masterTypeId" and "amount". In my report footer I have an String field "#UnboundString30". The formula is:
(Local NumberVar r;
r := 0;
WhileReadingRecords;
if{Table.masterTypeId}=2
then r := r + {Table.amount})
The Formula Workshop will save and close. When I run the report #UnboundString30 is always 0 despite there being many masterTypeIds of 2 and many amounts. It appears to me that I need to tell my field that it should be the value of r but I don't know how to do that?
Thanks!
It looks like you are missing the return. Try to add on line, like this:
Local NumberVar r;
r := 0;
WhileReadingRecords;
if{Table.masterTypeId}=2
then r := r + {Table.amount};
r; //this is the return
I need to obtain an average value of my input signals from mitsubishi input module Q64AD. I'm working in GX Works 2 in structured text.
This is how i used to obtain average value in Codesys:
timer_sr(IN:= NOT timer_sr.Q , PT:= T#5s );
SUM1:= SUM1 + napr1;
Nsum:=Nsum + 1;
IF timer_sr.Q THEN
timer_sr(IN:= NOT timer_sr.Q , PT:= T#5s);
outsr := SUM1 /Nsum;
Nsum := 0;
SUM1 := 0;
END_IF;
napr1 - is value from module
This piece of code is not working in GX Works 2, and i think because SUM1 is not an INT data type, but just a Word[signed] type.
Is there a way to make SUM1 an INT type or may be there is another logic to that solution?
In other platforms it should work but the compiler gives a warning so I guess it will still compile? Of course, if the value is negative there will be problems.
You can convert a WORD to INT by the IEC function WORD_TO_INT. I'm not sure how well your system follows the standard but if it does, try the following:
WORD_TO_INT(SUM1). If the SUM1 > 65535 then there will be problems as the upper bound of INTis 32767.
If this doesn't help, could you provide more details? How is it not workin?
Ps. The WORD is unsigned data type, not signed as you wrote.
I am working on a property of a given set of natural numbers and it seems difficult to compute. I build a function 'fun' which takes two inputs, one is the cardinal value and another is the set. If the set is empty then fun should return 0 because fun depends on the product of the set and fun on all subsets of the complement set.
For clarification here is an example:
S is a set given S={1,2,3,4}. The function fun(2,S) is defined as
fun(2,S)=prod({1,2})*[fun(1,{3}) + fun(1,{4}) + fun(2,{3,4})] +
prod({1,3})*[fun(1,{2}) + fun(1,{4}) + fun(2,{2,4})] +
prod({1,4})*[fun(1,{3}) + fun(1,{2}) + fun(2,{2,3})] +
prod({2,3})*[fun(1,{4}) + fun(1,{1}) + fun(2,{1,4})] +
prod({2,4})*[fun(1,{1}) + fun(1,{3}) + fun(2,{3,1})] +
prod({3,4})*[fun(1,{1}) + fun(1,{2}) + fun(2,{1,2})]
prod is defined as the product of all elements in a set, for example
prod({1,2})=2;
prod({3,2})=6
I am trying to compute the function fun using recursive method in MATLAB but it's not working. The base case is the cardinal value should be more than zero that means there should be at least one element in the set other wise prod will be zero and fun will return zero.
Update Pseudo code:
fun(i,S)
if |S|=1 && i!=0
return prod(S)
else if i==0
return 0
else
prod(subset s', s' is a subset of S and |s'|=i)*(sum over fun((for i=1 to m),{S-s'}), m=|S-s'|) //I don't know how to write code for this part and need help.
end if
end fun
prod(s)
n=|s|
temp=1
for i=1 to n
temp *=s(i) //s(1) is the 1st element of s
end for
return temp
end prod
Thanks.
With the pseudo code you added to your question it's nearly impossible to implement the function. Everything is put into one line which is incomplete (at least the outer sum is missing).
1) Formalize your algorithm in a way it can be used to implement. The following pseudo code is probably not correct because I don't exactly know what you want, but it should give an idea how to do it.
fun(i,S)
if i==0
return 0
else if |S|=1
return S
else
r=0
for s1 in subsets of S with size i
f=0
for s2 in subsets of setdiff(S,s') with size <=i
f=f+fun(s2,|s2|)
end
r=r+prod(s1)*f
end for
end if
end fun
2) use arrays [1,2,3,4] instead of cells {1,2,3,4}
3) prod is a built-in function, no need to reimplement it.
I am trying to convert this statement into a SQL command-- for testing and learning purposes.
The code in a formula in basic syntax is
if IsNumeric (Mid(Trim(X), 1)) Then
formula = UCASE(TRIM(MID(X, (INSTR (X, " ", 1) +1)))))
ELSE
formula = X.
What am I supposed to use instead of formula in the sql command and should this be done in sql command for efficiency? Even if I am not supposed to, I would like to know how to write this in sql command.
So far I have
CASE ISNUMERIC (Mid(Trim(X), 1)) Then
[BLANK] = UCASE(TRIM(MID(X, (CHARINDEX (X, " ", 1) +1)))))
ELSE
[BLANK] = X
EPV
Sample of Data. So when I import/link data from the back end. My X Field looks like a variety of:
1. 'zzabc123 - The Red Car'
2. 'abc123 - The Black Car'
3. 'The green car'
I want to create a SQL code where:
CHECK X (zzabc123 - the red car) to see if there is a zzabc123 in the front
if it is numeric, then cut out the front part - by using charindex to find where the space starts and grab only 'the red car'.
If it is not numeric, just clean up the DATA part
END CASE
After that case evaluate the DATA and use another CASE to find key words to use a generalized term for reporting.
RESULTS
RED CAR
BLACK CAR
GREEN CAR
SELECT *,
CASE ISNUMERIC(Mid(Trim(X), 1)) Then
UCASE(TRIM(MID(X, (CHARINDEX (X, " ", 1) +1)))))
ELSE X
END
CASE WHEN X LIKE '%RED%'
THEN 'RED CAR'
ELSE X LIKE '%BLACK%;
THEN 'BLACK CAR'
ELSE X LIKE '%GREEN%'
THEN 'GREEN CAR'
ELSE XY
END
FROM XTABLE`
Without knowing the specifics of your data and what you are trying to accomplish, it's next to impossible to accurately give you a SQL sample.
That said, here is a conversion of most of your functions to their SQL-Server equivalents; if you can look up items like the SUBSTRING() and CHARINDEX() functions, and adapt the sample to do whatever it is your requirements dictate, this should get you started.
SELECT
CASE
WHEN ISNUMERIC(SUBSTRING(LTRIM(RTRIM(X)), 1,1)) = 0
THEN UPPER(LTRIM(RTRIM(SUBSTRING(X, CHARINDEX(X, ' ')+1, LEN(X) - CHARINDEX(X, ' ')))))
ELSE 'X'
END
FROM MyTable
Try this:
CASE
WHEN ISNUMERIC (Mid(Trim(X), 1)) THEN
UCASE(TRIM(MID(X, (CHARINDEX (X, " ", 1) +1)))))
ELSE
X
END
i am receiving "A Subscript Must be Between 1 and the Size of the Array" for below code in crytal report. Please help!
'To Use Multiline if Basic Syntax was used
Shared Cust_Ids() As Number
Shared Page_No() As Number
Shared Cust_Ids2() As Number
Shared Page_No2() As Number
Local m As Number
Local i As Number
Local j As Number
Local Cnt As Number
j=1
i=1
Cnt=Count({CL_Index_Page_ttx.Customer_ID})
For i=1 To Cnt
If i <=1000 Then
If Cust_Ids(i) - {CL_Index_Page_ttx.Customer_ID} = 0 Then
m = Page_No(i)
i=Cnt+1
End If
Else
If Cust_Ids2(j) - {CL_Index_Page_ttx.Customer_ID} = 0 Then
m = Page_No2(j)
i=Cnt+1
Else
j=j+1
End If
End If
Next i
Formula=m
It looks like you may have the same problem as this question.
When assigning variables, use := instead of =, and see if that solves your problem.