I did attach <dict.txt> as a txt file in my sent email, but it seems to have 
been stripped … (I thought .txt files were ok to the mailing list now ??).

I’ll try once more here, if not people can ask me and I’ll email them or put on 
a google drive and share with whoever requests.

Thanks Rob, plain txt file here (again)
/- 3.3 Data Structures
/- Lists are a fundamental data type, used to build dictionaries and then (SQL) 
tables.  

/- 3.3.1        Lists
/- A list is created naturally by stringing together a series of atoms that can 
be either of the same type,
/- or of dissimilar type. As shown in 3.1 a list may be constructed of 
different type atoms using (;;;).

q)type 00011b                   /- A boolean list (1 or 0 only)
1h

q)type `Iverson                 /- This is a single symbol (an atom)
-11h

q)type `Iverson`Whitney         /- A list of symbols
11h

q)type (`Iverson`Whitney;1920 1957)  /- List of lists
0h

q)type each (`Iverson`Whitney;1920 1957) /- See type of each
11 6h

q)type each (`Iverson`Whitney;("APL";"kdb");1920 1957) /- Lists can be nested
11 0 6h

/- 3.3.2 Assignemnt and Indexing
/- Results may be assigned to a name (variable) for later use using “:” as 
shown below.
/- Objects in q may be indexed using [ ] notation, starting with position 0, 1, 
2 …

q)names:`Iverson`McCarthy`Whitney
q)born:1920 1927 1957

q)names[2 0]                             /- Index (reference) by position 
(start from 0)
`Whitney`Iverson

q)born[2 0]
1957 1920

/- 3.3.3        Dictionaries
/- A dictionary is a set of (key - value) pairs of equal length, created using 
“!”.

q)show d1:names!born            /- names are the key, born are the values
Iverson | 1920
McCarthy| 1927
Whitney | 1957

q)d1[`Whitney`Iverson]          /- index a dictionary by key (name) to see 
values
1957 1920

q)d1 where d1<>1927           /- value in d1 not equal to 1927
1920 1957

/- 3.3.4        Tables
/- A special dictionary is one where all keys have values that are lists of 
equal length (often call n-tuples).
/- The flip (transpose) of this dictionary returns a Table.
/- Tables may also be created using an explicit notation.  Both approaches are 
shown below.

q)flip d1                               /- d1 does not conform as values are 
atoms (not equal length lists)
'type

q)show d2:`Last`First`Born!(names;`Ken`John`Arthur;born)
Last | Iverson McCarthy Whitney
First| Ken     John     Arthur
Born | 1920    1927     1957

q)show t:flip d2                        /- Values are now equal length lists so 
can be 'flipped' (transposed)
Last     First  Born
--------------------
Iverson  Ken    1920
McCarthy John   1927
Whitney  Arthur 1957

q)t2:([]Last:names;First:`Ken`John`Arthur;Born:1920 1927 1957)
q)t2                          /- can also be produced using explicit notation 
Last     First  Born
--------------------
Iverson  Ken    1920
McCarthy John   1927
Whitney  Arthur 1957

q)t~t2                          /- “~” returns 1 if both variables match 
exactly
1b

q)select First,Born from t2     /- Can now use SQL to select from t or t2
First  Born
-----------
Ken    1920
John   1927
Arthur 1957

q)flip t2      /- flipping a table back, converts it back to the dictionary
Last | Iverson McCarthy Whitney
First| Ken     John     Arthur
Born | 1920    1927     1957

q)t2           /- Back to the Table view ...
Last     First  Born
--------------------
Iverson  Ken    1920
McCarthy John   1927
Whitney  Arthur 1957

q)t[0 2]             /- Can view a subset of rows of the table, here 2 rows
Last    First  Born
-------------------
Iverson Ken    1920
Whitney Arthur 1957

q)t[2]                /- But a single row displays as a dictionary
Last | `Whitney
First| `Arthur
Born | 1957

q)reverse t2          /- Can apply primitives to the table to produce a table
Last     First  Born
--------------------
Whitney  Arthur 1957
McCarthy John   1927
Iverson  Ken    1920

q)t2,reverse t2       /- Or catenate 2 tables to produce a larger table
Last     First  Born
--------------------
Iverson  Ken    1920
McCarthy John   1927
Whitney  Arthur 1957
Whitney  Arthur 1957
McCarthy John   1927
Iverson  Ken    1920



> On 2 Feb 2022, at 12:16 am, 'Rob Hodgkinson' via Programming 
> <programm...@jsoftware.com> wrote:
> 
> Dictionaries in q/k are actually a core part of the language, not just a tool 
> or package.
> <snip>

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to