Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- KX Community
- :
- Discussion Forums
- :
- q-bies
- :
- how to create temp variable inside $[]

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Options

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2022.04.13 09:56 PM

if i use $[] for if how can i add temp variable there? my understanding is it is either condition or the return value. so how can i put the below logic into this? suppose i have only input x and the if else logic below.

$[x=`a;5; ?]

if x = 'a' return 5

if x='b'

y = x*x +5

if( y > 6 ) return 6

if(y>9) return 10

1 ACCEPTED SOLUTION

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2022.04.13 10:33 PM

You can do something like the following:

```
q)f:{[x] $[x~`a;5;x~`b;[y:6*6;$[y<6;6;y>9;10;y]];x]}
q)f
{[x] $[x~`a;5;x~`b;[y:6*6;$[y<6;6;y>9;10;y]];x]}
q)f `a
5
q)f `b
10
```

I would use a function (as above) or a lambda (anonymous function), that way your temp variable will only remain for the scope of the function i.e. in the example above, **y **does not exist in my global namespace after execution. You can read more about conditional scope here

You can see if x is b in the above we have square brackets [ ] which defines a contained block of code to run if the statement is true. See more here

Please find more on the extended if-else / switch statement here

2 REPLIES 2

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2022.04.13 10:33 PM

You can do something like the following:

```
q)f:{[x] $[x~`a;5;x~`b;[y:6*6;$[y<6;6;y>9;10;y]];x]}
q)f
{[x] $[x~`a;5;x~`b;[y:6*6;$[y<6;6;y>9;10;y]];x]}
q)f `a
5
q)f `b
10
```

I would use a function (as above) or a lambda (anonymous function), that way your temp variable will only remain for the scope of the function i.e. in the example above, **y **does not exist in my global namespace after execution. You can read more about conditional scope here

You can see if x is b in the above we have square brackets [ ] which defines a contained block of code to run if the statement is true. See more here

Please find more on the extended if-else / switch statement here

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

2022.04.14 08:42 PM

thanks a lot!

Main Office Contacts

**EMEA**

Tel: +44 (0)28 3025 2242

**AMERICAS**

Tel: +1 (212) 447 6700

**APAC**

Tel: +61 (0)2 9236 5700

Useful Information

Resources

Popular Links

Follow Us

KX. All Rights Reserved.

KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.