I want to first break a string to list of strings and based on a condition I want to return an item of a list as long.
Func:{[x]
Temp:vs "-" x;
if["AAA" ~ Temp[0];:"J"$Temp[1];:"J"$Temp[2]]
}
Func["AAA-809-AXSDF"]
/This function returns 809 but when I do:
809 ~ Func["AAA-809-AXSDF"]
It returns 0b
This means it's not converting list item to long. Please suggest
CodePudding user response:
There are a few errors in your code:
1: [x]
is not necessary
2: vs "-" x
should be "-" vs x
3: if["AAA" ~ Temp[0];:"J"$Temp[1];:"J"$Temp[2]]
this statement if true always returns :"J"$Temp[1]
, :"J"$Temp[2]
will never be executed. I think what you need is the conditional $
operator
q)func:{"J"$ $["AAA"~first a:"-"vs x;a 1;a 2]}
q)809~func["AAA-809-AXSDF"]
1b
q)111~func["AAB-AXSDF-111"]
1b
CodePudding user response:
If I got the logic right, following code solves the issue:
{x: "-"vs x; "J"$ $["AAA"~x 0;x 1; x 2]}"AAA-809-AXSDF"