How to format a number to 2 digits in mule4? - mule

I have tried followings;
vars.counter as Number {format:'00'}
vars.counter as Number {format:'##'}
vars.counter as String {format:'00'}
vars.counter as String {format:'##'}
None of the above making 1 to 01
How can i do this in mule4?

Numbers (integers, floating point) don't have format DataWeave, like in many other languages. You have to convert to a String with the desired pattern. I tried the following combinations:
%dw 2.0
output application/json
---
[
1 as String {format:'##'},
1 as String {format:'00'},
1 as String {format:'#0'}
// , 1 as String {format:'0#'} ERROR!
]
Output:
[
"1",
"01",
"1"
]
Only the all zeros combination gives the desired result.

Related

How to make orderBy() order numbers after alphabetic characters?

While trying to sort data in ascending order in DataWeave, I've noticed that if the input for orderBy() contains strings that start with numbers and alphabetic characters like ["6", "7", "a", "8"], then the result is ["6","7","8","a"].
Is there a way that we can have strings which are starting with alphabetic characters before numbers?
By default in Unicode digits are before alphabet characters. You can use the criteria parameter of orderBy() to change the way numbers are going to be compared with characters. For example adding character { as a prefix which is a character that is just ufter z (lowercase z), then numbers will be after any alphabet characters.
Example:
%dw 2.0
output application/json
import isNumeric from dw::core::Strings
---
payload orderBy (if (isNumeric($)) "{" ++ $ else $ )
Output:
[
"a",
"6",
"7",
"8"
]

How to replace the list of decimal numbers in a string with another list of decimal numbers?

I need to replace the list of decimal numbers in a string with another list of decimal numbers. The following is a first try, that changes all decimal numbers with the same decimal number:
>>> re.sub (r"[-+]?\d*\.\d+f?", "1.0", "hello 1.2 3.4")
'hello 1.0 1.0'
I need something like my_replace below:
>>> my_replace (r"[-+]?\d*\.\d+f?", [1.0, 2.0], "hello 1.2 3.4")
'hello 1.0 2.0'
How can i implement my_replace with python's re module?
I don't think that you can use a list as replacement variables and iterate over them. So it can't handle unhashable objects (this is what python is complaining about). But it wouln'd be able to handle numerics as well (so it would need a list of strings but this is obviously hypothetical xD)
I would just loop over the string and compy everything that is not a decimal number to a new string and replacing the decimal numbers found.
text = "hello 1.2 3.4 don't replace an integer: 9 but this decimal number is too much: 0.0 (so use last value!)"
new_numbers = [42, 3.1415926535]
new_text = ''
idx_last = 0
for i, tx in enumerate(re.finditer(r'[-+]?\d*\.\d+f?', text)):
# add text before the number
new_text += tx.string[idx_last:tx.start()]
# add new number (but ensure that your are not overflowing the list of new numbers
new_text += str(new_numbers[min([i, len(new_numbers) - 1])])
# update text index
idx_last = tx.end()
# update remaining part of the text
new_text += text[idx_last:]
"hello 42 3.1415926535 don't replace an integer: 9 but this decimal number is too much: 3.1415926535 (so use last value!)"
Wrap it to a function and you have your my_replace() =)

compare two arrays in dataweave2.0

i want code to compare two arrays and determine if they are equal or not irrespective of their order
[a,b,c] compared to [a, b,c ] should be true
[a,b,c] compare to [a,c,b] should be true as well.
i tried using the diff function from dataweave 2.0 but it works only if the parameters are Json objects not for arrays.
as #George mentioned a simple orderBy fixed my issue
import diff from dw::util::Diff
%dw 2.0
output application/json
---
{
result: diff(payload.array orderBy $, vars.array orderBy $).matches
}
fixed the issue.
You can use the Diff module with the unordered property
import diff from dw::util::Diff
%dw 2.0
output application/json
---
{
result: diff(payload.array, vars.array, {unordered: true}).matches
}

How to format integer as string with 2 digits?

I would like to format an integer 9 to "09" and 25 to "25".
How can this be done?
You can use either of these options:
The "0" Custom Specifier
value.ToString("00")
String.Format("{0:00}", value)
The Decimal ("D") Standard Format Specifier
value.ToString("D2")
String.Format("{0:D2}", value)
For more information:
Custom Numeric Format Strings
Standard Numeric Format Strings
If its just leading zero's that you want, you can use this:
value.tostring.padleft("0",2)
value.ToString().PadLeft(2, '0'); // C#
If you have 2 digits, say 25 for example, you will get "25" back....if you have just one digit, say 9 for example, you will get "09"....It is worth noting that this gives you a string back, and not an integer, so you may need to cast this later on in your code.
String formate is the best way to do that. It's will only add leading zero for a single length. 9 to "09" and 25 to "25".
String.format("%02d", value)
Bonus:
If you want to add multiple leading zero 9 to "0009" and 1000 to "1000". That's means you want a string for 4 indexes so the condition will be %04d.
String.format("%04d", value)
I don't know the exact syntax. But in any language, it would look like this.
a = 9
aString =""
if a < 10 then
aString="0" + a
else
aString = "" + a
end if

Octave keyboard input function to filter concatenated string and integer?

if we write 12wkd3, how to choose/filter 123 as integer in octave?
example in octave:
A = input("A?\n")
A?
12wkd3
A = 123
while 12wkd3 is user keyboard input and A = 123 is the expected answer.
assuming that the general form you're looking for is taking an arbitrary string from the user input, removing anything non-numeric, and storing the result it as an integer:
A = input("A? /n",'s');
A = int32(str2num(A(isdigit(A))));
example output:
A?
324bhtk.p89u34
A = 3248934
to clarify what's written above:
in the input statement, the 's' argument causes the answer to get stored as a string, otherwise it's evaluated by Octave first. most inputs would produce errors, others may be interpreted as functions or variables.
isdigit(A) produces a logical array of values for A with a 1 for any character that is a 0-9 number, and 0 otherwise.
isdigit('a1 3 b.') = [0 1 0 1 0 0 0]
A(isdigit(A)) will produce a substring from A using only those values corresponding to a 1 in the logical array above.
A(isdigit(A)) = 13
that still returns a string, so you need to convert it into a number using str2num(). that, however, outputs a double precision number. so finally to get it to be an integer you can use int32()