How to do zero padding ?

664 views
Skip to first unread message

bigbug

unread,
Jun 14, 2012, 6:07:58 AM6/14/12
to Kdb+ Personal Developers
I have a table like this (select sym,price from hlytb):

sym price
------------
837 9.22
600461 7.74
600129 7.36
423 6.68

and I want the sym to be zero padded to 6 digits like this :

sym price
------------
000837 9.22
600461 7.74
600129 7.36
000423 6.68

How to do ?

And this "a: `837; (count string a)_"000000",string a" works , but
"select ((count string sym)_"000000",string sym) by sym from hlytb"
not work as I want , why ?

Thanks,

Terry Lynch

unread,
Jun 14, 2012, 6:15:58 AM6/14/12
to personal...@googlegroups.com
This should work for the padding

q)select "0"^-6$string sym,price from hlytb
sym      price
--------------
"000837" 9.22
"600461" 7.74
"600129" 7.36
"000423" 6.68

Haven't looked at your other question but it looks like you're applying an atomic operation to a list (column) so you'd need an "each" in there somewhere.

Terry



--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To post to this group, send email to personal...@googlegroups.com.
To unsubscribe from this group, send email to personal-kdbpl...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/personal-kdbplus?hl=en.


bigbug

unread,
Jun 14, 2012, 6:30:32 AM6/14/12
to Kdb+ Personal Developers
thanks,

the other question is when I try "select ((count string
sym)_"000000",string sym) by sym from hlytb" , it shows:

sym | sym
------| ----------------------------
423 | "0" "0" "0" "0" "0" "423"
600062| "0" "0" "0" "0" "0" "600062"
600129| "0" "0" "0" "0" "0" "600129"
837 | "0" "0" "0" "0" "0" "837"

and I try "select count string sym by sym from hlytb" , it shows

sym | sym
------| ---
423 | 1
600062| 1
600129| 1
837 | 1

but what I want is:

sym | sym
------| ---
423 | 3
600062| 6
600129| 6
837 | 3

don't know why the "count" can't calculate the character number of
"string sym" .
> >http://groups.google.com/group/personal-kdbplus?hl=en.- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Terry Lynch

unread,
Jun 14, 2012, 6:42:53 AM6/14/12
to personal...@googlegroups.com
If you want to do it that way you'll have to either modify the operation to apply to lists of strings (rather than a single string) or else apply the operation to each string. For example

q)select {(count string x)_"000000",string x} each sym by sym from hlytb
sym   | sym
------| --------
423   | "000423"
837   | "000837"
600129| "600129"
600461| "600461"

Again, for the counting you need to apply it to each of the strings, otherwise its just counting the whole list (column) as one

 q)select count each string sym by sym from hlytb
sym   | sym
------| ---
423   | 3
837   | 3
600129| 6
600461| 6

Terry

bigbug

unread,
Jun 14, 2012, 6:47:52 AM6/14/12
to Kdb+ Personal Developers
broaden my understanding of q , many thanks...
> > > >http://groups.google.com/group/personal-kdbplus?hl=en.-隐藏被引用文字 -

Hemant Verma

unread,
Jun 14, 2012, 6:48:34 AM6/14/12
to personal...@googlegroups.com
You can try floowing....
 
q)select sym, count each string sym from hlytb

sym         sym1
----------------------
837             3
423             3
600129        6
600461        6
60046178    8
Thanks
Hemant
Reply all
Reply to author
Forward
0 new messages