Tcl Source Code

Hex Artifact Content
Login

Artifact da08e5a9a343a67a6ab1129695559c9e27b0e95a:

Attachment "dict-filter.tip" to ticket [2370575fff] added by lars_h 2008-12-02 20:34:43.
0000: 54 49 50 3a 20 20 20 20 20 20 20 20 20 20 20 20  TIP:            
0010: 0d 54 69 74 6c 65 3a 20 20 20 20 20 20 20 20 20  .Title:         
0020: 20 4d 75 6c 74 69 70 6c 65 20 64 69 63 74 20 66   Multiple dict f
0030: 69 6c 74 65 72 20 70 61 74 74 65 72 6e 73 0d 56  ilter patterns.V
0040: 65 72 73 69 6f 6e 3a 20 20 20 20 20 20 20 20 0d  ersion:        .
0050: 41 75 74 68 6f 72 3a 20 20 20 20 20 20 20 20 20  Author:         
0060: 4c 61 72 73 20 48 65 6c 6c 73 74 72 c2 9a 6d 20  Lars Hellstršm 
0070: 3c 4c 61 72 73 20 64 6f 74 20 48 65 6c 6c 73 74  <Lars dot Hellst
0080: 72 6f 6d 20 61 74 20 72 65 73 69 64 65 6e 73 65  rom at residense
0090: 74 20 64 6f 74 20 6e 65 74 3e 0d 53 74 61 74 65  t dot net>.State
00a0: 3a 20 20 20 20 20 20 20 20 20 20 44 72 61 66 74  :          Draft
00b0: 0d 54 79 70 65 3a 20 20 20 20 20 20 20 20 20 20  .Type:          
00c0: 20 50 72 6f 6a 65 63 74 0d 56 6f 74 65 3a 20 20   Project.Vote:  
00d0: 20 20 20 20 20 20 20 20 20 0d 54 63 6c 2d 56 65           .Tcl-Ve
00e0: 72 73 69 6f 6e 3a 20 20 20 20 38 2e 36 0d 43 72  rsion:    8.6.Cr
00f0: 65 61 74 65 64 3a 20 20 20 20 20 20 20 20 32 37  eated:        27
0100: 2d 4e 6f 76 2d 32 30 30 38 0d 4b 65 79 77 6f 72  -Nov-2008.Keywor
0110: 64 73 3a 20 20 20 20 20 20 20 64 69 63 74 20 66  ds:       dict f
0120: 69 6c 74 65 72 2c 20 73 65 74 20 69 6e 74 65 72  ilter, set inter
0130: 73 65 63 74 69 6f 6e 0d 50 6f 73 74 2d 48 69 73  section.Post-His
0140: 74 6f 72 79 3a 20 20 20 0d 0d 0d 7e 20 41 62 73  tory:   ...~ Abs
0150: 74 72 61 63 74 0d 0d 54 68 65 20 27 27 27 6b 65  tract..The '''ke
0160: 79 27 27 27 20 61 6e 64 20 27 27 27 76 61 6c 75  y''' and '''valu
0170: 65 27 27 27 20 66 6f 72 6d 73 20 6f 66 20 27 27  e''' forms of ''
0180: 27 64 69 63 74 20 66 69 6c 74 65 72 27 27 27 20  'dict filter''' 
0190: 61 72 65 20 67 65 6e 65 72 61 6c 69 73 65 64 20  are generalised 
01a0: 0d 74 6f 20 61 6c 6c 6f 77 20 61 6e 20 61 72 62  .to allow an arb
01b0: 69 74 72 61 72 79 20 6e 75 6d 62 65 72 20 6f 66  itrary number of
01c0: 20 70 61 74 74 65 72 6e 73 2e 0d 0d 0d 7e 20 53   patterns....~ S
01d0: 70 65 63 69 66 69 63 61 74 69 6f 6e 0d 0d 54 68  pecification..Th
01e0: 65 20 74 77 6f 20 27 27 27 64 69 63 74 20 66 69  e two '''dict fi
01f0: 6c 74 65 72 27 27 27 20 63 6f 6d 6d 61 6e 64 20  lter''' command 
0200: 66 6f 72 6d 73 0d 0d 20 3e 20 27 27 27 64 69 63  forms.. > '''dic
0210: 74 20 66 69 6c 74 65 72 27 27 27 20 27 27 64 69  t filter''' ''di
0220: 63 74 69 6f 6e 61 72 79 27 27 20 27 27 27 6b 65  ctionary'' '''ke
0230: 79 27 27 27 20 27 27 70 61 74 74 65 72 6e 27 27  y''' ''pattern''
0240: 0d 0d 20 3e 20 27 27 27 64 69 63 74 20 66 69 6c  .. > '''dict fil
0250: 74 65 72 27 27 27 20 27 27 64 69 63 74 69 6f 6e  ter''' ''diction
0260: 61 72 79 27 27 20 27 27 27 76 61 6c 75 65 27 27  ary'' '''value''
0270: 27 20 27 27 70 61 74 74 65 72 6e 27 27 0d 0d 61  ' ''pattern''..a
0280: 72 65 20 67 65 6e 65 72 61 6c 69 73 65 64 20 74  re generalised t
0290: 6f 0d 0d 20 3e 20 27 27 27 64 69 63 74 20 66 69  o.. > '''dict fi
02a0: 6c 74 65 72 27 27 27 20 27 27 64 69 63 74 69 6f  lter''' ''dictio
02b0: 6e 61 72 79 27 27 20 27 27 27 6b 65 79 27 27 27  nary'' '''key'''
02c0: 20 3f 27 27 70 61 74 74 65 72 6e 27 27 20 2e 2e   ?''pattern'' ..
02d0: 2e 3f 0d 0d 20 3e 20 27 27 27 64 69 63 74 20 66  .?.. > '''dict f
02e0: 69 6c 74 65 72 27 27 27 20 27 27 64 69 63 74 69  ilter''' ''dicti
02f0: 6f 6e 61 72 79 27 27 20 27 27 27 76 61 6c 75 65  onary'' '''value
0300: 27 27 27 20 3f 27 27 70 61 74 74 65 72 6e 27 27  ''' ?''pattern''
0310: 20 2e 2e 2e 3f 0d 0d 61 6e 64 20 74 68 65 20 72   ...?..and the r
0320: 65 73 75 6c 74 73 20 61 72 65 20 74 68 65 20 73  esults are the s
0330: 75 62 2d 64 69 63 74 69 6f 6e 61 72 69 65 73 20  ub-dictionaries 
0340: 6f 66 20 74 68 6f 73 65 20 6b 65 79 73 20 61 6e  of those keys an
0350: 64 20 76 61 6c 75 65 73 20 0d 72 65 73 70 65 63  d values .respec
0360: 74 69 76 65 6c 79 20 77 68 69 63 68 20 6d 61 74  tively which mat
0370: 63 68 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  ch at least one 
0380: 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 73 2e  of the patterns.
0390: 0d 0d 0d 7e 20 52 61 74 69 6f 6e 61 6c 65 0d 0d  ...~ Rationale..
03a0: 41 6c 74 68 6f 75 67 68 20 74 68 65 72 65 20 61  Although there a
03b0: 72 65 20 27 27 27 64 69 63 74 27 27 27 20 73 75  re '''dict''' su
03c0: 62 63 6f 6d 6d 61 6e 64 73 20 77 68 69 63 68 20  bcommands which 
03d0: 61 6c 6c 6f 77 20 64 65 6c 65 74 69 6e 67 20 73  allow deleting s
03e0: 6f 6d 65 20 6b 65 79 73 20 0d 66 72 6f 6d 20 61  ome keys .from a
03f0: 20 64 69 63 74 69 6f 6e 61 72 79 20 28 27 27 27   dictionary ('''
0400: 64 69 63 74 20 72 65 6d 6f 76 65 27 27 27 29 20  dict remove''') 
0410: 61 6e 64 20 69 6e 73 65 72 74 69 6e 67 20 73 6f  and inserting so
0420: 6d 65 20 6b 65 79 73 20 69 6e 74 6f 20 61 20 0d  me keys into a .
0430: 64 69 63 74 69 6f 6e 61 72 79 20 28 27 27 27 64  dictionary ('''d
0440: 69 63 74 20 72 65 70 6c 61 63 65 27 27 27 29 2c  ict replace'''),
0450: 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 72   there is no dir
0460: 65 63 74 20 77 61 79 20 6f 66 20 72 65 71 75 65  ect way of reque
0470: 73 74 69 6e 67 20 0d 74 68 65 20 73 75 62 2d 64  sting .the sub-d
0480: 69 63 74 69 6f 6e 61 72 79 20 77 68 69 63 68 20  ictionary which 
0490: 6f 6e 6c 79 20 68 61 73 20 6b 65 79 73 20 66 72  only has keys fr
04a0: 6f 6d 20 61 20 67 69 76 65 6e 20 6c 69 73 74 3b  om a given list;
04b0: 20 69 66 20 77 65 20 74 68 69 6e 6b 20 0d 6f 66   if we think .of
04c0: 20 6f 6e 6c 79 20 74 68 65 20 73 65 74 20 6f 66   only the set of
04d0: 20 6b 65 79 73 20 69 6e 20 74 68 65 20 64 69 63   keys in the dic
04e0: 74 69 6f 6e 61 72 79 2c 20 74 68 65 6e 20 77 65  tionary, then we
04f0: 20 68 61 76 65 20 73 75 62 63 6f 6d 6d 61 6e 64   have subcommand
0500: 73 20 66 6f 72 20 0d 73 65 74 20 6d 69 6e 75 73  s for .set minus
0510: 20 61 6e 64 20 73 65 74 20 75 6e 69 6f 6e 2c 20   and set union, 
0520: 62 75 74 20 6e 6f 6e 65 20 66 6f 72 20 73 65 74  but none for set
0530: 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 2e 20 0d   intersection. .
0540: 41 20 73 69 74 75 61 74 69 6f 6e 20 77 68 65 72  A situation wher
0550: 65 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20  e this would be 
0560: 75 73 65 66 75 6c 20 69 73 20 74 68 61 74 20 74  useful is that t
0570: 68 65 20 6f 70 74 69 6f 6e 20 64 69 63 74 69 6f  he option dictio
0580: 6e 61 72 79 20 66 6f 72 20 0d 61 20 68 69 67 68  nary for .a high
0590: 2d 6c 65 76 65 6c 20 70 72 6f 63 65 64 75 72 65  -level procedure
05a0: 20 63 61 6e 20 63 6f 6e 74 61 69 6e 20 6f 70 74   can contain opt
05b0: 69 6f 6e 73 20 6d 65 61 6e 74 20 74 6f 20 62 65  ions meant to be
05c0: 20 70 61 73 73 65 64 20 6f 6e 20 74 6f 20 0d 6c   passed on to .l
05d0: 6f 77 65 72 20 6c 65 76 65 6c 20 63 6f 6d 6d 61  ower level comma
05e0: 6e 64 73 2c 20 61 6e 64 20 69 74 20 69 73 20 6e  nds, and it is n
05f0: 65 63 65 73 73 61 72 79 20 74 6f 20 65 78 74 72  ecessary to extr
0600: 61 63 74 20 74 68 65 20 73 75 62 64 69 63 74 69  act the subdicti
0610: 6f 6e 61 72 79 20 0d 6f 66 20 6f 70 74 69 6f 6e  onary .of option
0620: 73 20 74 68 61 74 20 74 68 65 20 6c 6f 77 65 72  s that the lower
0630: 20 6c 65 76 65 6c 20 63 6f 6d 6d 61 6e 64 20 77   level command w
0640: 6f 75 6c 64 20 61 63 63 65 70 74 20 28 73 69 6e  ould accept (sin
0650: 63 65 20 70 61 73 73 69 6e 67 20 6f 6e 65 20 0d  ce passing one .
0660: 77 68 69 63 68 20 69 73 20 6e 6f 74 20 73 75 70  which is not sup
0670: 70 6f 72 74 65 64 20 77 6f 75 6c 64 20 63 61 75  ported would cau
0680: 73 65 20 69 74 20 74 6f 20 74 68 72 6f 77 20 61  se it to throw a
0690: 6e 20 65 72 72 6f 72 29 2e 0d 0d 54 68 65 72 65  n error)...There
06a0: 20 69 73 20 6f 66 20 63 6f 75 72 73 65 20 61 6c   is of course al
06b0: 72 65 61 64 79 20 74 68 65 20 27 27 27 64 69 63  ready the '''dic
06c0: 74 20 66 69 6c 74 65 72 27 27 27 20 63 6f 6d 6d  t filter''' comm
06d0: 61 6e 64 2c 20 77 68 69 63 68 20 69 6e 64 65 65  and, which indee
06e0: 64 20 0d 72 65 74 75 72 6e 73 20 61 20 73 75 62  d .returns a sub
06f0: 64 69 63 74 69 6f 6e 61 72 79 20 6f 66 20 61 6e  dictionary of an
0700: 20 65 78 69 73 74 69 6e 67 20 64 69 63 74 69 6f   existing dictio
0710: 6e 61 72 79 2c 20 62 75 74 20 69 74 73 20 27 27  nary, but its ''
0720: 27 6b 65 79 27 27 27 20 66 6f 72 6d 20 0d 6f 6e  'key''' form .on
0730: 6c 79 20 61 63 63 65 70 74 73 20 6f 6e 65 20 27  ly accepts one '
0740: 27 27 73 74 72 69 6e 67 20 6d 61 74 63 68 27 27  ''string match''
0750: 27 20 70 61 74 74 65 72 6e 20 61 6e 64 20 74 68  ' pattern and th
0760: 65 72 65 66 6f 72 65 20 63 61 6e 6e 6f 74 20 62  erefore cannot b
0770: 65 20 75 73 65 64 20 0d 74 6f 20 65 2e 67 2e 20  e used .to e.g. 
0780: 73 65 6c 65 63 74 20 61 6c 6c 20 74 68 72 65 65  select all three
0790: 20 6f 66 20 2d 66 6f 6f 2c 20 2d 62 61 72 2c 20   of -foo, -bar, 
07a0: 61 6e 64 20 2d 62 61 7a 20 28 69 74 20 63 6f 75  and -baz (it cou
07b0: 6c 64 20 73 65 6c 65 63 74 20 62 6f 74 68 20 0d  ld select both .
07c0: 2d 62 61 72 20 61 6e 64 20 2d 62 61 7a 20 74 68  -bar and -baz th
07d0: 72 6f 75 67 68 20 74 68 65 20 70 61 74 74 65 72  rough the patter
07e0: 6e 20 2d 62 61 5b 72 7a 5d 2c 20 62 75 74 20 74  n -ba[rz], but t
07f0: 68 61 74 27 73 20 6e 65 69 74 68 65 72 20 63 6f  hat's neither co
0800: 6d 6d 6f 6e 20 6e 6f 72 20 0d 70 61 72 74 69 63  mmon nor .partic
0810: 75 6c 61 72 6c 79 20 72 65 61 64 61 62 6c 65 29  ularly readable)
0820: 2e 20 48 6f 77 65 76 65 72 2c 20 69 6e 20 6d 61  . However, in ma
0830: 6e 79 20 69 6e 73 74 61 6e 63 65 73 20 77 68 65  ny instances whe
0840: 72 65 20 74 68 69 73 20 6b 69 6e 64 20 6f 66 20  re this kind of 
0850: 0d 70 61 74 74 65 72 6e 20 69 73 20 75 73 65 64  .pattern is used
0860: 20 28 6e 6f 74 61 62 6c 79 20 27 27 27 67 6c 6f   (notably '''glo
0870: 62 27 27 27 2c 20 27 27 27 6e 61 6d 65 73 70 61  b''', '''namespa
0880: 63 65 20 65 78 70 6f 72 74 27 27 27 2c 20 61 6e  ce export''', an
0890: 64 20 0d 27 27 27 73 77 69 74 63 68 27 27 27 29  d .'''switch''')
08a0: 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  , it is possible
08b0: 20 74 6f 20 67 69 76 65 20 73 65 76 65 72 61 6c   to give several
08c0: 20 73 75 63 68 20 70 61 74 74 65 72 6e 73 20 61   such patterns a
08d0: 6e 64 20 68 61 76 65 20 69 74 20 0d 69 6e 74 65  nd have it .inte
08e0: 72 70 72 65 74 65 64 20 61 73 20 74 68 65 20 75  rpreted as the u
08f0: 6e 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 74 74  nion of the patt
0900: 65 72 6e 73 2e 20 57 65 72 65 20 74 68 61 74 20  erns. Were that 
0910: 74 68 65 20 63 61 73 65 20 77 69 74 68 20 0d 27  the case with .'
0920: 27 27 64 69 63 74 20 66 69 6c 74 65 72 27 27 27  ''dict filter'''
0930: 2c 20 74 68 65 20 22 2d 66 6f 6f 2c 20 2d 62 61  , the "-foo, -ba
0940: 72 2c 20 61 6e 64 20 2d 62 61 7a 22 20 70 72 6f  r, and -baz" pro
0950: 62 6c 65 6d 20 63 6f 75 6c 64 20 62 65 20 73 6f  blem could be so
0960: 6c 76 65 64 20 61 73 20 0d 65 61 73 69 6c 79 20  lved as .easily 
0970: 61 73 0d 0d 7c 20 20 64 69 63 74 20 66 69 6c 74  as..|  dict filt
0980: 65 72 20 24 6f 70 74 73 20 6b 65 79 20 2d 66 6f  er $opts key -fo
0990: 6f 20 2d 62 61 72 20 2d 62 61 7a 0d 0d 77 68 69  o -bar -baz..whi
09a0: 63 68 20 69 73 20 63 6f 6d 70 61 72 61 62 6c 65  ch is comparable
09b0: 20 74 6f 0d 0d 7c 20 20 64 69 63 74 20 72 65 6d   to..|  dict rem
09c0: 6f 76 65 20 24 6f 70 74 73 20 2d 66 6f 6f 20 2d  ove $opts -foo -
09d0: 62 61 72 20 2d 62 61 7a 0d 7c 20 20 64 69 63 74  bar -baz.|  dict
09e0: 20 72 65 70 6c 61 63 65 20 24 6f 70 74 73 20 2d   replace $opts -
09f0: 66 6f 6f 20 31 20 2d 62 61 72 20 6f 66 66 20 2d  foo 1 -bar off -
0a00: 62 61 7a 20 34 32 0d 0d 61 6e 64 20 6d 75 63 68  baz 42..and much
0a10: 20 6e 69 63 65 72 20 74 68 61 6e 20 74 68 65 20   nicer than the 
0a20: 27 27 27 73 63 72 69 70 74 27 27 27 20 63 6f 75  '''script''' cou
0a30: 6e 74 65 72 70 61 72 74 0d 0d 7c 20 20 64 69 63  nterpart..|  dic
0a40: 74 20 66 69 6c 74 65 72 20 24 6f 70 74 73 20 73  t filter $opts s
0a50: 63 72 69 70 74 20 7b 6b 65 79 20 76 61 6c 7d 20  cript {key val} 
0a60: 7b 0d 7c 20 20 20 20 20 3a 3a 74 63 6c 3a 3a 6d  {.|     ::tcl::m
0a70: 61 74 68 6f 70 3a 3a 69 6e 20 24 6b 65 79 20 7b  athop::in $key {
0a80: 2d 66 6f 6f 20 2d 62 61 72 20 2d 62 61 7a 7d 0d  -foo -bar -baz}.
0a90: 7c 20 20 7d 0d 0d 49 66 20 74 68 65 20 27 27 27  |  }..If the '''
0aa0: 6b 65 79 27 27 27 20 66 6f 72 6d 20 69 73 20 67  key''' form is g
0ab0: 65 6e 65 72 61 6c 69 73 65 64 20 6c 69 6b 65 20  eneralised like 
0ac0: 74 68 69 73 2c 20 74 68 65 6e 20 69 74 20 73 65  this, then it se
0ad0: 65 6d 73 20 61 70 70 72 6f 70 72 69 61 74 65 20  ems appropriate 
0ae0: 0d 74 6f 20 61 6c 73 6f 20 67 65 6e 65 72 61 6c  .to also general
0af0: 69 73 65 20 74 68 65 20 27 27 27 76 61 6c 75 65  ise the '''value
0b00: 27 27 27 20 66 6f 72 6d 20 69 6e 20 74 68 65 20  ''' form in the 
0b10: 73 61 6d 65 20 77 61 79 20 74 6f 20 6b 65 65 70  same way to keep
0b20: 20 74 68 65 20 0d 73 79 6d 6d 65 74 72 79 2c 20   the .symmetry, 
0b30: 65 76 65 6e 20 74 68 6f 75 67 68 20 49 20 68 61  even though I ha
0b40: 76 65 20 6e 6f 20 69 6d 6d 65 64 69 61 74 65 20  ve no immediate 
0b50: 75 73 65 2d 63 61 73 65 20 66 6f 72 20 74 68 61  use-case for tha
0b60: 74 20 66 65 61 74 75 72 65 2e 0d 0d 53 69 6e 63  t feature...Sinc
0b70: 65 20 69 74 20 69 73 20 67 65 6e 65 72 61 6c 6c  e it is generall
0b80: 79 20 67 6f 6f 64 20 74 6f 20 44 6f 20 4e 6f 74  y good to Do Not
0b90: 68 69 6e 67 20 47 72 61 63 65 66 75 6c 6c 79 2c  hing Gracefully,
0ba0: 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 73 79 6e   the command syn
0bb0: 74 61 78 20 69 73 20 0d 61 6c 73 6f 20 67 65 6e  tax is .also gen
0bc0: 65 72 61 6c 69 73 65 64 20 74 6f 20 61 6c 6c 6f  eralised to allo
0bd0: 77 20 74 68 65 20 63 61 73 65 20 6f 66 20 6e 6f  w the case of no
0be0: 20 70 61 74 74 65 72 6e 73 20 61 74 20 61 6c 6c   patterns at all
0bf0: 2e 0d 0d 0d 7e 20 52 65 6a 65 63 74 65 64 20 61  ....~ Rejected a
0c00: 6c 74 65 72 6e 61 74 69 76 65 73 0d 0d 41 20 6d  lternatives..A m
0c10: 6f 72 65 20 64 69 72 65 63 74 20 77 61 79 20 6f  ore direct way o
0c20: 66 20 6d 65 65 74 69 6e 67 20 74 68 65 20 6d 6f  f meeting the mo
0c30: 74 69 76 61 74 69 6e 67 20 6e 65 65 64 20 77 6f  tivating need wo
0c40: 75 6c 64 20 62 65 20 61 20 63 6f 6d 6d 61 6e 64  uld be a command
0c50: 20 0d 27 27 27 64 69 63 74 20 73 65 6c 65 63 74   .'''dict select
0c60: 27 27 27 20 77 69 74 68 20 74 68 65 20 73 61 6d  ''' with the sam
0c70: 65 20 73 79 6e 74 61 78 20 61 73 20 27 27 27 64  e syntax as '''d
0c80: 69 63 74 20 72 65 6d 6f 76 65 27 27 27 20 28 6e  ict remove''' (n
0c90: 6f 20 70 61 74 74 65 72 6e 20 0d 6d 61 74 63 68  o pattern .match
0ca0: 69 6e 67 29 20 62 75 74 20 6c 6f 67 69 63 20 72  ing) but logic r
0cb0: 65 76 65 72 73 65 64 2e 20 54 68 69 73 20 77 6f  eversed. This wo
0cc0: 75 6c 64 20 68 6f 77 65 76 65 72 20 62 65 20 73  uld however be s
0cd0: 6f 20 63 6c 6f 73 65 20 74 6f 20 0d 27 27 27 64  o close to .'''d
0ce0: 69 63 74 20 66 69 6c 74 65 72 27 27 27 20 2e 2e  ict filter''' ..
0cf0: 2e 20 27 27 27 6b 65 79 27 27 27 20 74 68 61 74  . '''key''' that
0d00: 20 65 78 74 65 6e 64 69 6e 67 20 74 68 65 20 73   extending the s
0d10: 79 6e 74 61 78 20 6f 66 20 74 68 65 20 6c 61 74  yntax of the lat
0d20: 74 65 72 20 0d 73 65 65 6d 65 64 20 6d 6f 72 65  ter .seemed more
0d30: 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0d 0d 41   appropriate...A
0d40: 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 74 6f  n alternative to
0d50: 20 61 6c 6c 6f 77 69 6e 67 20 6d 75 6c 74 69 70   allowing multip
0d60: 6c 65 20 70 61 74 74 65 72 6e 73 20 77 69 74 68  le patterns with
0d70: 20 27 27 27 64 69 63 74 20 66 69 6c 74 65 72 27   '''dict filter'
0d80: 27 27 20 63 6f 75 6c 64 20 0d 62 65 20 74 6f 20  '' could .be to 
0d90: 61 6c 6c 6f 77 20 61 20 72 65 67 75 6c 61 72 20  allow a regular 
0da0: 65 78 70 72 65 73 73 69 6f 6e 20 70 61 74 74 65  expression patte
0db0: 72 6e 2c 20 73 69 6e 63 65 20 74 68 65 20 75 6e  rn, since the un
0dc0: 69 6f 6e 20 6f 66 20 74 77 6f 20 72 65 67 75 6c  ion of two regul
0dd0: 61 72 20 0d 6c 61 6e 67 75 61 67 65 73 20 69 73  ar .languages is
0de0: 20 61 67 61 69 6e 20 61 20 72 65 67 75 6c 61 72   again a regular
0df0: 20 6c 61 6e 67 75 61 67 65 2e 20 41 6e 79 20 73   language. Any s
0e00: 79 6e 74 61 78 20 74 68 61 74 20 63 6f 75 6c 64  yntax that could
0e10: 20 62 65 20 70 69 63 6b 65 64 20 66 6f 72 20 0d   be picked for .
0e20: 74 68 61 74 20 77 6f 75 6c 64 20 68 6f 77 65 76  that would howev
0e30: 65 72 20 6f 6e 20 6f 6e 65 20 68 61 6e 64 20 61  er on one hand a
0e40: 6c 72 65 61 64 79 20 62 65 20 72 61 74 68 65 72  lready be rather
0e50: 20 63 6c 6f 73 65 20 74 6f 0d 0d 7c 20 20 64 69   close to..|  di
0e60: 63 74 20 66 69 6c 74 65 72 20 24 6f 70 74 73 20  ct filter $opts 
0e70: 73 63 72 69 70 74 20 7b 6b 65 79 20 76 61 6c 7d  script {key val}
0e80: 20 7b 72 65 67 65 78 70 20 24 52 45 20 24 6b 65   {regexp $RE $ke
0e90: 79 7d 0d 0d 61 6e 64 20 6f 6e 20 74 68 65 20 6f  y}..and on the o
0ea0: 74 68 65 72 20 69 74 20 77 6f 75 6c 64 20 62 65  ther it would be
0eb0: 20 72 61 74 68 65 72 20 64 69 66 66 69 63 75 6c   rather difficul
0ec0: 74 20 74 6f 20 72 65 61 64 2c 20 61 73 20 74 68  t to read, as th
0ed0: 65 20 72 65 67 75 6c 61 72 20 0d 65 78 70 72 65  e regular .expre
0ee0: 73 73 69 6f 6e 20 63 6f 72 72 65 73 70 6f 6e 64  ssion correspond
0ef0: 69 6e 67 20 74 6f 20 22 2d 66 6f 6f 20 6f 72 20  ing to "-foo or 
0f00: 2d 62 61 72 20 6f 72 20 2d 62 61 7a 22 20 69 73  -bar or -baz" is
0f10: 0d 0d 7c 20 20 5e 28 2d 66 6f 6f 7c 2d 62 61 72  ..|  ^(-foo|-bar
0f20: 7c 2d 62 61 7a 29 24 0d 0d 77 68 69 63 68 20 69  |-baz)$..which i
0f30: 74 20 69 73 20 74 65 6d 70 74 69 6e 67 20 62 75  t is tempting bu
0f40: 74 20 69 6e 63 6f 72 72 65 63 74 20 74 6f 20 73  t incorrect to s
0f50: 69 6d 70 6c 69 66 79 20 74 6f 20 22 2d 66 6f 6f  implify to "-foo
0f60: 7c 2d 62 61 72 7c 2d 62 61 7a 22 2e 0d 0d 0d 7e  |-bar|-baz"....~
0f70: 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
0f80: 4e 6f 74 65 73 0d 0d 41 6e 20 69 6d 70 6c 65 6d  Notes..An implem
0f90: 65 6e 74 61 74 69 6f 6e 20 65 78 69 73 74 73 20  entation exists 
0fa0: 28 69 74 27 73 20 61 20 76 65 72 79 20 74 72 69  (it's a very tri
0fb0: 76 69 61 6c 20 74 6f 20 6d 6f 64 69 66 79 20 27  vial to modify '
0fc0: 27 27 64 69 63 74 20 66 69 6c 74 65 72 27 27 27  ''dict filter'''
0fd0: 20 0d 2e 2e 2e 20 27 27 27 76 61 6c 75 65 27 27   .... '''value''
0fe0: 27 20 74 6f 20 77 6f 72 6b 20 74 68 69 73 20 77  ' to work this w
0ff0: 61 79 3a 20 6a 75 73 74 20 61 64 64 20 61 6e 20  ay: just add an 
1000: 69 6e 6e 65 72 20 6c 6f 6f 70 20 6f 76 65 72 20  inner loop over 
1010: 74 68 65 20 6c 69 73 74 20 6f 66 20 0d 70 61 74  the list of .pat
1020: 74 65 72 6e 73 29 3b 20 73 65 65 20 53 46 20 70  terns); see SF p
1030: 61 74 68 20 23 32 33 37 30 35 37 35 2e 0d 0d 57  ath #2370575...W
1040: 68 61 74 20 6d 69 67 68 74 20 62 65 20 74 72 69  hat might be tri
1050: 63 6b 79 20 69 73 20 74 68 65 20 63 61 73 65 20  cky is the case 
1060: 6f 66 20 27 27 27 64 69 63 74 20 66 69 6c 74 65  of '''dict filte
1070: 72 27 27 27 20 2e 2e 2e 20 27 27 27 6b 65 79 27  r''' ... '''key'
1080: 27 27 2c 20 73 69 6e 63 65 20 0d 74 68 69 73 20  '', since .this 
1090: 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61 6e  currently has an
10a0: 20 6f 70 74 69 6d 69 73 61 74 69 6f 6e 20 66 6f   optimisation fo
10b0: 72 20 74 68 65 20 63 61 73 65 20 6f 66 20 61 20  r the case of a 
10c0: 70 61 74 74 65 72 6e 20 77 69 74 68 6f 75 74 20  pattern without 
10d0: 67 6c 6f 62 20 0d 6d 65 74 61 63 68 61 72 61 63  glob .metacharac
10e0: 74 65 72 73 20 74 68 61 74 20 77 6f 75 6c 64 20  ters that would 
10f0: 62 65 20 76 65 72 79 20 64 65 73 69 72 61 62 6c  be very desirabl
1100: 65 20 74 6f 20 6b 65 65 70 20 66 6f 72 20 74 68  e to keep for th
1110: 65 20 6d 6f 74 69 76 61 74 69 6e 67 20 0d 75 73  e motivating .us
1120: 65 2d 63 61 73 65 20 6f 66 20 73 65 6c 65 63 74  e-case of select
1130: 69 6e 67 20 73 70 65 63 69 66 69 63 20 6b 65 79  ing specific key
1140: 73 20 66 72 6f 6d 20 61 20 64 69 63 74 69 6f 6e  s from a diction
1150: 61 72 79 2e 20 54 68 65 20 6e 61 74 75 72 61 6c  ary. The natural
1160: 20 77 61 79 20 74 6f 20 0d 64 6f 20 74 68 61 74   way to .do that
1170: 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 6d 61 6b   would be to mak
1180: 65 20 74 68 65 20 6c 6f 6f 70 20 6f 76 65 72 20  e the loop over 
1190: 70 61 74 74 65 72 6e 73 20 74 68 65 20 6f 75 74  patterns the out
11a0: 65 72 20 6c 6f 6f 70 20 61 6e 64 20 74 68 65 20  er loop and the 
11b0: 0d 6c 6f 6f 70 20 6f 76 65 72 20 64 69 63 74 69  .loop over dicti
11c0: 6f 6e 61 72 79 20 65 6e 74 72 69 65 73 20 74 68  onary entries th
11d0: 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 2c 20 77 68  e inner loop, wh
11e0: 69 63 68 20 69 73 20 6f 6e 6c 79 20 65 6e 74 65  ich is only ente
11f0: 72 65 64 20 69 66 20 74 68 65 20 0d 63 75 72 72  red if the .curr
1200: 65 6e 74 20 70 61 74 74 65 72 6e 20 63 6f 6e 74  ent pattern cont
1210: 61 69 6e 73 20 6d 65 74 61 63 68 61 72 61 63 74  ains metacharact
1220: 65 72 73 2e 20 53 75 63 68 20 61 6e 20 6f 70 74  ers. Such an opt
1230: 69 6d 69 73 61 74 69 6f 6e 20 77 6f 75 6c 64 20  imisation would 
1240: 0d 68 6f 77 65 76 65 72 20 68 61 76 65 20 74 68  .however have th
1250: 65 20 73 63 72 69 70 74 2d 6c 65 76 65 6c 2d 76  e script-level-v
1260: 69 73 69 62 6c 65 20 63 6f 6e 73 65 71 75 65 6e  isible consequen
1270: 63 65 20 6f 66 20 68 61 76 69 6e 67 20 74 68 65  ce of having the
1280: 20 6b 65 79 73 20 73 68 6f 77 20 0d 75 70 20 69   keys show .up i
1290: 6e 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74  n the order of t
12a0: 68 65 20 70 61 74 74 65 72 6e 73 20 72 61 74 68  he patterns rath
12b0: 65 72 20 74 68 61 6e 20 74 68 65 20 6f 72 64 65  er than the orde
12c0: 72 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61  r of the origina
12d0: 6c 20 0d 64 69 63 74 69 6f 6e 61 72 79 2c 20 73  l .dictionary, s
12e0: 6f 20 69 74 20 6d 61 79 20 62 65 20 61 20 67 6f  o it may be a go
12f0: 6f 64 20 69 64 65 61 20 74 6f 20 61 6c 73 6f 20  od idea to also 
1300: 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65 63 69  explicitly speci
1310: 66 79 20 74 68 61 74 20 0d 27 27 27 64 69 63 74  fy that .'''dict
1320: 20 66 69 6c 74 65 72 27 27 27 20 64 6f 65 73 20   filter''' does 
1330: 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 20 6b 65  not guarantee ke
1340: 79 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ys in the result
1350: 20 74 6f 20 62 65 20 69 6e 20 74 68 65 20 73 61   to be in the sa
1360: 6d 65 20 0d 6f 72 64 65 72 20 61 73 20 69 6e 20  me .order as in 
1370: 74 68 65 20 69 6e 70 75 74 20 64 69 63 74 69 6f  the input dictio
1380: 6e 61 72 79 2e 0d 0d 49 6e 64 65 65 64 2c 20 61  nary...Indeed, a
1390: 20 27 27 27 64 69 63 74 20 66 69 6c 74 65 72 27   '''dict filter'
13a0: 27 27 20 2e 2e 2e 20 27 27 27 6b 65 79 27 27 27  '' ... '''key'''
13b0: 20 74 68 61 74 20 72 65 6f 72 64 65 72 73 20 6b   that reorders k
13c0: 65 79 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  eys according to
13d0: 20 0d 69 74 73 20 70 61 74 74 65 72 6e 20 61 72   .its pattern ar
13e0: 67 75 6d 65 6e 74 73 20 63 6f 75 6c 64 20 73 6f  guments could so
13f0: 6d 65 74 69 6d 65 73 20 62 65 20 75 73 65 66 75  metimes be usefu
1400: 6c 20 69 6e 20 69 6e 74 65 72 61 63 74 69 76 65  l in interactive
1410: 20 73 69 74 75 61 74 69 6f 6e 73 2c 20 0d 61 73   situations, .as
1420: 20 61 20 77 61 79 20 6f 66 20 67 65 74 74 69 6e   a way of gettin
1430: 67 20 73 65 6c 65 63 74 65 64 20 6b 65 79 73 20  g selected keys 
1440: 75 70 20 66 72 6f 6d 20 69 6e 20 61 20 64 69 63  up from in a dic
1450: 74 69 6f 6e 61 72 79 3a 0d 0d 7c 20 20 73 65 74  tionary:..|  set
1460: 20 44 20 7b 2d 62 61 7a 20 30 20 2d 62 61 72 20   D {-baz 0 -bar 
1470: 31 20 2d 66 6f 6f 20 32 7d 0d 7c 20 20 64 69 63  1 -foo 2}.|  dic
1480: 74 20 66 69 6c 74 65 72 20 24 44 20 6b 65 79 20  t filter $D key 
1490: 2d 66 6f 6f 20 2d 62 61 72 20 2a 0d 0d 4f 6e 20  -foo -bar *..On 
14a0: 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20  the other hand, 
14b0: 74 68 69 73 20 65 66 66 65 63 74 20 63 61 6e 20  this effect can 
14c0: 6d 6f 73 74 6c 79 20 62 65 20 6f 62 74 61 69 6e  mostly be obtain
14d0: 65 64 20 74 68 72 6f 75 67 68 20 75 73 65 20 6f  ed through use o
14e0: 66 20 0d 27 27 27 64 69 63 74 20 6d 65 72 67 65  f .'''dict merge
14f0: 27 27 27 20 61 6c 72 65 61 64 79 3a 0d 0d 7c 20  ''' already:..| 
1500: 20 64 69 63 74 20 6d 65 72 67 65 20 7b 2d 66 6f   dict merge {-fo
1510: 6f 20 78 20 2d 62 61 72 20 78 7d 20 24 44 0d 0d  o x -bar x} $D..
1520: 0d 7e 20 43 6f 70 79 72 69 67 68 74 0d 0d 54 68  .~ Copyright..Th
1530: 69 73 20 64 6f 63 75 6d 65 6e 74 20 68 61 73 20  is document has 
1540: 62 65 65 6e 20 70 6c 61 63 65 64 20 69 6e 20 74  been placed in t
1550: 68 65 20 70 75 62 6c 69 63 20 64 6f 6d 61 69 6e  he public domain
1560: 2e 20 0d                                         . .