I was trying the if test for the first time, well actually so does with function.
Here's the script:
function trial()
I = input("f INPUT > Manually input frequency value? (yes/no):");
if I = "yes";
f = input("Please input the frequency value : \n")
elseif I = "no";
f = randi([100 1000],5,5)
endif
O = 2*pi*f;
fprintf("%.2f \n",O);
plot(f,O);
xlabel("Frequency");
ylabel("Angular Frequency");
end
f INPUT > Manually input frequency value? (yes/no):"no"
Please input the frequency value :
There are 2 things that I don't understand:
Why do I have to write the condition with quotation marks? (i.e "yes" not yes or "no" not no).
Why the 'no' condition ran the input command when it should've been randi?
Can someone show me how it should be done?
I = "yes" assigns the string "yes" to the variable I. To make a comparison, use == like this: I == "yes". But this comparison will only work if the two strings are the same length, and will return an array, not a single equality value. Use strcmp to compare strings: if strcmp(I, "yes").
The input function parses what the user types, so that typing 3 will result in the number 3, not the string "3". If you add a second input argument like this: input("prompt","s") then it will not parse the input, and return a string. The user will then be able to type no. See the docs.
Related
I Need help for fetch records from table using Go.
My Problem is that i'm writing MySQL query and add another where clause i.e HPhone number, Here HPhone number inserted in data base with format like 999-999-9999.And i passed this HPhone Number in format like 9999999999. which is not matching with correct data base field value. And i used SUBSTRING for add hyphen between numbers but it does not get records but when i passed like 999-999-9999 without SUBSTRING it return records.
Here i demonstrate how i used this.
strQry = `SELECT * from table WHERE Depot = ?`
if HPhone != "" {
strQry += ` AND HPhone = ?`
}
queryArgs := []interface{}{RouteAvailability.Depot}
if HPhone != "" {
queryArgs = append(queryArgs, "SUBSTRING("+HPhone+",1,3)"+"-"+"SUBSTRING("+HPhone+",4,3)"+"-"+"SUBSTRING("+HPhone+",7,4)")
}
Help would be appreciated.
Thanks in advance.
Instead of SUBSTRING you can use REPLACE like so:
queryArgs := []interface{}{RouteAvailability.Depot}
if HPhone != "" {
strQry += ` AND REPLACE(HPhone, '-', '') = ?`
queryArgs = append(queryArgs, HPhone)
}
If possible I would suggest you normalize your data, i.e. decide on a canonical format for a particular data type and everytime your program receives some input that contains that data type you format it into its canonical form, that way you can avoid having to deal with SUBSTRING, or REPLACE, or multiple inconsistent formats etc.
This won't work as you are using prepared statements, and the argument you are building when HPhone is not empty will be used in escaped form - so when executing the query, it won't compare the HPhone values with the computed result of some substring, but with a string containing SUBSTRING(9999...
In my Access query, I have the query using a VBA function to figure the value that goes in the query field.
In the form, if the stringval textbox has a value, then I want to compute it, but if not, it should remain empty (null).
Function GetValue(stringval, numval)
Dim result
stringval= stringval & ""
result= IIf(stringval<> "", numval* 1.5, Null)
GetValue = Int(result)
End Function
Now, I have a form that uses this query, and on the form is a textbox that displays the query value. I want the value to be formatted with commas in the numbers for easy reading. Everything I've tried so far does not show any commas.
I've tried:
used Standard for the Format > Formatfor the textbox (in properties)
putting #,###.### in the textbox Format value
putting #,##0.0## in the textbox Format value
changing Data > Text Format but it only gives me Plain Text and Rich Text - no option for numbers.
returning a double from the function
Note: if I don't use a custom VBA function, and write the formula directly into the query, then it does display commas. But when I move the formula into the function then the commas are lost.
What do I do?
[update]
I tried Gustav's solutions and since they didn't work for me, I added those as items to my "what I've tried" list above.
Also, if I look at the query in datasheet view, the number values sort alphabetically instead of by the size of the value. When I used the forumulae directly in the query instead of using functions, it sorted by the value of the number. I hope this is a clue.
Numbers carries no format. A format is applied when displayed only.
But be sure to return a Double if not Null:
Function GetValue(stringval, numval)
Dim result
If stringval & "" <> "" Then
result = Int(CDbl(numval) * 1.5)
Else
result = Null
End If
GetValue = result
End Function
Then apply your Format to the textbox
#,##0.0##
Or force a formatted string to be returned:
If stringval & "" <> "" Then
result = Format(Int(CDbl(numval) * 1.5), "#,##0.0##")
Else
result = Null
End If
and skip formatting of the textbox.
The solution is this: the function has to be declared as a double.
That allows the query's datasheet view to know it is displaying numbers - and so you can set the field's format to Standard for the comma to display. This also allows the form to know it has a number and it will display the comma there, too. I knew it had to do with double, but didn't realize before that the function needed to be declared as such.
Function GetValue(stringval, numval) as double '<----THIS!!!!
Dim result
If stringval & "" <> "" Then
result = numval * 1.5
Else
result = 0 `<--can't return null here; use nz function in control source for textbox
End If
GetValue = int(result) 'to remove decimals
End Function
The problem I was having was in some of my functions I need to return double or null, because I wanted textboxes to remain blank if they contained no data. Now, at least I know how to make the numbers generated by functions to display commas.
And here is how to deal with the fact that you can't return null as the value of a double. The function is originally from here.
Put this function in a module so it is public, and then in the control source for the textbox, instead of just putting the field value, put Zn(fieldvalue). This works like a charm (although using functions in the control source seems to have a delay on the form display). This way you can keep the underlying value as a double and still get commas to display in both the form and the query whilst keeping the field blank if necessary.
Public Function Zn(pvar)
' Return null if input is zero or ""
If IsNull(pvar) Then
Zn = Null
ElseIf IsNumeric(pvar) Then
If pvar = 0 Then
Zn = Null
Else
Zn = pvar
End If
Else
If Len(pvar) = 0 Then
Zn = Null
Else
Zn = pvar
End If
End If
End Function
Hi I am transforming JSON to JSON with Dataweave in Mulesoft 3.8.4. I am working with the following lines in JSON (part of a larger file)
..
"someField": "12",
"otherField": "5441",
..
I want to format 'someField' to a zero left padded string with a total length of 4 (0012). I also want to concat that value to the otherField that also needs to be left padded but with a total length of 6.
To prevent the padding and concatenating to fail I have to check if the field is present, not empty and it has to be numeric.
I have the following code:
"My Number": someField as :number as :string {format: "0000"} ++ otherField as :number as :string {format: "0000"}
when somefield != null
and someField != ""
and someField is :number
and otherField != null
and otherField != ""
and otherField is :number
otherwise "",
But this fails because 'is :number' returns false because actually the value is a string. Checking something like 'and someField as :number is :number' also fails when the value is empty. What's the best way to check?
Thanx for helping out.
John
Okay I figured it out myself. The part that was mostly in the way was to see if the value was a number. As the node in JSON could be non existing, empty or a text it was difficult to be able to do these tests in one when-otherwise.
What was also a requirement is that there always should be a returning value. So skipping when null was not an option.
Changing the number test into a regex helped. To also make the code more readable I also added some functions that I could also re-use.
The code now looks like this:
%function repeat(char, times) (char ++ repeat(char, times - 1)) when times > 0 otherwise ""
%function pad(field, count) field as :number as :string {format: repeat('0', count)}
%function toNumber(value, count) (pad(value, count)
when value matches /^\d+$/
otherwise "")
...
"My Number" : "" when someField == null
otherwise "" when otherField == null
otherwise toNumber(someField, 4) ++
toNumber(filterUpper(otherField), 6),
...
The repeat function gives a string of n-th repeated characters. The pad function left pads my number converted to string with zero's. In this function I use the repeat function to provide the format with the variable zero's string.
The toNumber is a check that see if we only use numbers.
Hope someone else can also benefit from this.
I had a similar question before, but this is a few steps beyond that so here we go:
I am trying to access values in a category in Tridion 2011. I am using the Razor TBB and using this code to do it:
#foreach (var keyword in Publication.MetaData.myCategory) {
#: Hello World!
}
I have set up a metadata schema with a field that has an xml name of "myCategory" attached to my publication. If I run this on the publication where myCategory is a Text field, this code works... kind of. It treats EACH character as a separate value of the keyword variable... so if I enter the text "one", what prints is "Hello World! Hello World! Hello World!", and if I just have "o" as the value, it prints "Hello World!".
Bizarre as that is (and I'd like to know why on that too), what I really want the field to be a "Values will be selected from a list" type of field, pointing to my category in Categories and keywords. When I do this, and the value of myCategory changes to the value of the item selected in the dropdown for this type of field instead of direct text entry, the code no longer works and gives this error:
Cannot implicitly convert type 'Tridion.Extensions.Mediators.Razor.Models.KeywordModel' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)
Please help me.
If you get your output repeated for every character in a string, you are clearly iterating over the characters in the string and not the other range of values that you expected.
I quickly checked the relevant code of the Razor mediator and its ToString method indeed returns the Title of the underlying RepositoryLocalObject.
http://code.google.com/p/razor-mediator-4-tridion/source/browse/trunk/Tridion.Extensions.Mediators.RazorMediator/Tridion.Extensions.Mediators.RazorMediator/Models/AbstractRepositoryLocalObject.cs
http://code.google.com/p/razor-mediator-4-tridion/source/browse/trunk/Tridion.Extensions.Mediators.RazorMediator/Tridion.Extensions.Mediators.RazorMediator/Models/KeywordModel.cs
http://code.google.com/p/razor-mediator-4-tridion/source/browse/trunk/Tridion.Extensions.Mediators.RazorMediator/Tridion.Extensions.Mediators.RazorMediator/Models/DynamicItemFields.cs
The code that handles KeywordFields in in the DynamicItemsFields.cs file:
else if (itemField is KeywordField)
{
KeywordField keywordField = (KeywordField)itemField;
if (keywordField.Definition.MaxOccurs == 1)
if (keywordField.Value == null)
_dictionary[key] = null;
else
_dictionary[key] = new KeywordModel(_engine, keywordField.Value);
else
{
List<KeywordModel> keywords = new List<KeywordModel>();
int i = 0;
foreach (Keyword k in keywordField.Values)
{
var kw = new KeywordModel(_engine, k);
kw.Index = i++;
kw.IsLast = Index == keywordField.Values.Count - 1;
keywords.Add(kw);
}
_dictionary[key] = keywords;
}
}
So it looks like the myCategory property will either be a KeywordModel object (if the KeywordField is single-value) or a List<KeywordModel> (if the KeywordField is marked as multi-value in the Schema). Is your myCategory field single value? Or multi-value?
If it is single-value, what type of output were your expecting? If you were expecting the list of allowed values (instead of the currently selected value), check if you can access it through myCategory.Definition somehow (which should be a regular TOM.NET KeywordFieldDefinition object).
I have the following:
Textfield called: WoNr
Table column called: Workorder
= DMax("[WoNr]","[Workorder]","[Workorder]") + 1
In the text field named WoNr I have entered the code above, I get an error.
Why is this?
Why are you using "[Workorder]" as the criterion (i.e., the last parameter)? Try the following:
= DMax("WoNr", "Workorder")
If this works, continue reading.
Now about the "+ 1" thing. You say that WoNr is a text field (rather than a numeric field). So, what do you want to get? Do you want to append "1" to the string (WoNr = "D1" => Result = "D11") or is WoNr actually a numeric value and you want to add 1? In any case, you should make your intention clear. For string concatenation, use &:
= DMax("WoNr", "Workorder") & "1"
for arithmetic operations, convert your text into an appropriate numeric data type first:
= CLng(DMax("WoNr", "Workorder")) + 1