I have a list of variable length (generated from a regexall) that I need to modify. I need the list length to remain the same, so I can't just filter out any list entry that doesn't have my parameters. I'm blanking on how to do it.
Example:
List: [" 7", "¥1", " 3", " D", " 7"]
Need to convert each entry that doesn't have a ¥ in it to be "0" and if it was a "¥" that will convert to a "+". So the new list should be. ["0", "+1", "0", "0", "0"]
Any ideas?
Hi @Kevin_Roberts , try this:
{mylist=[" 7", "¥1", " 3", " D", " 7"]}
{newlist=map(mylist, (item) -> replace(item, "¥", "+") if contains(item, "¥") else "0"); trim=yes}
{=newlist}
The key construct here is a ternary operator: X if condition else Y
. If condition
is true/yes, this outputs X
. Otherwise, it outputs Y
. Examples:
{=1 if yes else 0}
{=1 if no else 0}
1 Like
Any room for a If/Else here? I realize my list may possibly contain "-" which i'd also need to keep. I can't seem to figure out how to have it handle both the "¥" and the "-" then make everything else 0.
list=[" 7", "¥1", "-3", " D", " 7"]
I'd need ¥ to change to +. - would remain - but would need the number after it as well.
You can chain them like so:
{mylist=[" 7", "¥1", "-3", " D", " 7"]}
{newlist=map(mylist, (item) -> replace(item, "¥", "+") if startswith(item, "¥") else (item if startswith(item, "-") else "0")); trim=yes}
{=newlist}
As it looks infeasible, you can simplify using functions:
{mylist=[" 7", "¥1", "-3", " D", " 7"]}
{fix_nonyen=(item)->item if startswith(item, "-") else "0";trim=yes}
{fix_general=(item)->replace(item, "¥", "+") if startswith(item, "¥") else fix_nonyen(item);trim=yes}
{newlist=map(mylist, fix_general); trim=yes}
{=newlist}