Wenchi Liao

Just somewhere to stash things. I usually sign things as WL.


Longer snippets


short snippets.

slob This is a gussied up glob (aka slob). I first posted this on clt. The original thread can be found on google groups [1 ].

  proc slob {pattern str} {

      # super glob --> slob :)
      # returns 1 if pattern matches string; 0 otherwise
      # mimics glob style matching, with % to mean "words"
      # will probably have troubles with ranges...

      if {[string match *%* $pattern]} {
          set newpat "^[string map {* .* ? . % \\S+} $pattern]$"
          #puts $newpat
          return [regexp -- $newpat $str]
      } else {
          return [string match $pattern $str]
      }

  }

  % slob {hey, % there!}  "hey, you there!"
  1
  % slob {*hey, % there!*}  "blah hey, you there! blah"
  1
  % slob {hey, % there!}  "hey, there!"
  0
  % slob {hey, % there!}  "hey, 3-4 there!"
  1
  % slob {hey, % there!}  "hey, 3(4) there!"
  1
  % slob {hey, * there!}  "hey, 3(4) there!"
  1
  % slob {hey, * there!}  "hey, 3(4) there!" 

lfilter

I was working on some list-intensive stuff, and I kept using similar idioms to express the idea of filtering a list. So, lfilter. It takes a list, and a decider program. The program takes an a element of the list and returns a truth or false. Elements that are true become added to a new list.

  proc lfilter {args} {

      # filters a list. Takes a list, and a decider
      # command. The decider must return true or
      # false. No reliable error checking, decider
      # better be good...
      # the -n is for negation of the decider
      # returns a new list
      # ex:
      #   % proc s {a} { if {$a >0} { return 1 } else { return 0 }}
      #   % wl_utils::lfilter [list -2 -1 0 1 2] s
      #   1 2
      #   % wl_utils::lfilter -n [list -2 -1 0 1 2] s
      #   -2 -1 0

      set ret [list]
      set len [llength $args]
      set neg 0

      set usage_msg "listf ?-n? list decider"

      switch -- $len {
          2 {
              set lst [lindex $args 0]
              set cmd [lindex $args 1]
          }
          3 {
              if {[string match "-n*" [lindex $args 0]]} {
                  set neg 1
                  set lst [lindex $args 1]
                  set cmd [lindex $args 2]
              } else {
                  error "Unknown option: $usage_msg"
              }
          }
          default {
              error "Wrong number of arguments: $usage_msg"
          }
      }

      if {$neg} {
          foreach i $lst {
              if {! [eval $cmd $i]} {
                  lappend ret $i
              }
          }

      } else {
          foreach i $lst {
              if {[eval $cmd $i]} {
                  lappend ret $i
              }
          }
      }

      return $ret
  }

mydebug

On random debug missions, I like to know what proc my debug statements are coming from. Only for the truly ghetto sessions.

  proc mydebug {{msg ""}} {
      # debug string that puts in which proc it is being called from

      if {$msg == ""} {
          puts -nonewline stderr "DEBUG:[lindex [info level -1] 0]"
      } else {
          puts -nonewline stderr "DEBUG:[lindex [info level -1] 0]-->$msg\n"
      }
  }

AMG: Mmm, plenty of music-related stuff. Have you seen itunesdb? If you're interested, I can resume work on it, with your support. (I don't actually own an iPod.) -- Sorry, I'm not really interested in trying to reverse engineer the itunes db. COM is there for a reason...