首页 > 代码库 > newLISP list操作

newLISP list操作

构建一个list

注意要标记不要求值,比如用‘ 或者list

> (list "a" "b" "c")
("a" "b" "c")

或者

> ‘("a" "b" "c")
("a" "b" "c")


处理string list

在一个string list中用正则表达式查找

查找第一个匹配到的元素:

> (set ‘l ‘("WARNING: org.apache.hadoop.metrics.jvm.EventCounter is deprecated. Please use org.apache.hadoop.log.metrics.EventCounter in all the log4j.properties files." "recommend" "search" "test"))
("WARNING: org.apache.hadoop.metrics.jvm.EventCounter is deprecated. Please use org.apache.hadoop.log.metrics.EventCounter in all the log4j.properties files." 
 "recommend" "search" "test")
> (find "WARN" l 1)
0
> (find "WARN3" l 1)

注意,第三个参数用来表示用正则表达式查找,1表示大小写不敏感,0表示敏感。


用正则表达式查找并删除string list中的string

;; @param case-insensitive 1 for case-insensitive search or 0 for no special options                                                              
(define (remove str-list str-regex case-insensitive)
  (set ‘i (find str-regex str-list case-insensitive))
  (pop str-list i)
  str-list
  )

这样下面的函数利用上面的函数将hive命令返回的string list中不需要的过滤掉,只剩下数据库名称。

;; @syntax (HIVE:show-dbs)                                                                                                                        
(define (show-dbs)
  (set ‘r (exec (format "hive -e ‘show databases‘ 2>&1")))
  (set ‘r (STRING_LIST:remove r "^WARNING.+" 1))
  (set ‘r (STRING_LIST:remove r "^Logging.+" 1))
  (set ‘r (STRING_LIST:remove r "^Hive.+" 1))
  (set ‘r (STRING_LIST:remove r "OK" 1))
  (STRING_LIST:remove r "^Time taken.+" 1)
  )