首页 > 代码库 > 处理string list

处理string list


处理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

(context ‘STRING_LIST)

;; @param case-insensitive 1 for case-insensitive search or 0 for no special options
(define (remove str-list str-regex case-insensitive)
  (do-while i
	    (begin
	      (set ‘i (find str-regex str-list case-insensitive))
	      (if i
	      (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)
  )

处理string list