Baby steps in Q

Hello,

I’m trying to learn Q and to do so I’ve written a program to calculate the ammount of money found in a cash drawer. User is prompted to type the ammount of bank notes and coins and the  result is displayed in the console:

My first attempt was this:


tokens: 50000 20000 10000 5000 2000 1000 500 200 100 50 20 10 5 2 1

drawer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1"500 Euros: ";v:read0 0

drawer[0]:“I”$v

1"200 Euros: ";v:read0 0

drawer[1]:“I”$v

1"100 Euros: ";v:read0 0

drawer[2]:“I”$v

1"50 Euros: ";v:read0 0

drawer[3]:“I”$v

1"20 Euros: ";v:read0 0

drawer[4]:“I”$v

1"10 Euros: ";v:read0 0

drawer[5]:“I”$v

1"5 Euros: ";v:read0 0

drawer[6]:“I”$v

1"2 Euros: ";v:read0 0

drawer[7]:“I”$v

1"1 Euro: ";v:read0 0

drawer[8]:“I”$v

1"50 Cts: ";v:read0 0

drawer[9]:“I”$v

1"20 Cts: ";v:read0 0

drawer[10]:“I”$v

1"10 Cts: ";v:read0 0

drawer[11]:“I”$v

1"5 Cts: ";v:read0 0

drawer[12]:“I”$v

1"2 Cts: ";v:read0 0

drawer[13]:“I”$v

1"1 Cts: ";v:read0 0

drawer[14]:“I”$v





1"500 Euros: ";drawer[0]*tokens[0]%100;“asd”


1"200 Euros: ";

drawer[1]*tokens[1]%100


1"100 Euros: ";

drawer[2]*tokens[2]%100


1"50 Euros: ";

drawer[3]*tokens[3]%100


1"20 Euros: ";

drawer[4]*tokens[4]%100


1"10 Euros: ";

drawer[5]*tokens[5]%100


1"5 Euros: ";

drawer[6]*tokens[6]%100


1"2 Euros: ";

drawer[7]*tokens[7]%100


1"1 Euro: ";

drawer[8]*tokens[8]%100


1"50 Cts: ";

drawer[9]*tokens[9]%100


1"20 Cts: ";

drawer[10]*tokens[10]%100


1"10 Cts: ";

drawer[11]*tokens[11]%100


1"5 Cts: ";

drawer[12]*tokens[12]%100


1"2 Cts: ";

drawer[13]*tokens[13]%100


1"1 Cts: ";

drawer[14]*tokens[14]%100


1"Total: ";

sum drawer*tokens%100



Then I tried using while loops for input and output:


tokens: 50000 20000 10000 5000 2000 1000 500 200 100 50 20 10 5 2 1

drawer: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
STDOUT:-1
tmp:STDOUT""
tmp:STDOUT"Please type the amount of bank notes and coins:"
t:count tokens
while [t; tmp:STDOUT(string tokens[(count tokens)-t]%100)," Euros:";v:read0 0;drawer[(count tokens)-t]:"I"$v;t:t-1]
tmp:STDOUT""
tmp:STDOUT"RESULTS:"
t:count tokens
while [t; tmp:STDOUT(string tokens[(count tokens)-t]%100)," Euros:";tmp:STDOUT(string drawer[(count tokens)-t]*tokens[(count tokens)-t]%100);t:t-1]
tmp:STDOUT""
tmp:STDOUT"Total in Euros: ",string(sum drawer*tokens%100)


Comments will be appreciated, specially how to combine in a single line a strings and variables like:


Notes of 500 Euros:

----- – — ------ ------

| | | | |

| | | | | ____ Input field

| | | | _________ String 2

| | | ________________ Variable 2: string tokens[(count tokens)-t]%100

| | ____________________ String 1

| _______________________ Variable 1


Cheers



Francisco

In this version I’ve inserted the elements of both lists (tokens and drawer) using while loops:

STDOUT:-1

tokens: 5 2 1
while [(count tokens)-15; tokens:(10*tokens[0 1 2]),tokens]
drawer: enlist 0
while [(count drawer)-15; drawer:drawer,0]
tmp:STDOUT"Please type the amount of bank notes and coins:"
t:count tokens
while [t; tmp:STDOUT(string tokens[(count tokens)-t]%100)," Euros:";v:read0 0;drawer[(count tokens)-t]:"I"$v;t:t-1]
tmp:STDOUT""
tmp:STDOUT"RESULTS:"
t:count tokens
while [t; tmp:STDOUT(string tokens[(count tokens)-t]%100)," Euros:";tmp:STDOUT(string drawer[(count tokens)-t]*tokens[(count tokens)-t]%100);t:t-1]
tmp:STDOUT""
tmp:STDOUT"Total in Euros: ",string(sum drawer*tokens%100)

Cheers

Francisco

Now I’ve combined 2 while loops in one:

STDOUT:-1

tokens: 5 2 1

drawer: 0 0 0

while [(count tokens)-15; tokens:(10*tokens[0 1 2]),tokens;drawer:drawer,0 0 0]

tmp:STDOUT"Please type the amount of bank notes and coins:"

t:count tokens

while [t; tmp:STDOUT(string tokens[(count tokens)-t]%100)," Euros:";v:read0 0;drawer[(count tokens)-t]:“I”$v;t:t-1]

tmp:STDOUT""

tmp:STDOUT"RESULTS:"

t:count tokens

while [t; tmp:STDOUT(string tokens[(count tokens)-t]%100)," Euros:";tmp:STDOUT(string drawer[(count tokens)-t]*tokens[(count tokens)-t]%100);t:t-1]

tmp:STDOUT""

tmp:STDOUT"Total in Euros: ",string(sum drawer*tokens%100)

A good way to learn more would be to visit code.kx.com and read up (if you haven’t already), and also read sites linked to it i.e.?http://code.kx.com/wiki/Contrib?