如何在二维列表中更改值

时间:2014-09-05 07:29:01

标签: emacs elisp

假设我们有以下列表:

(setq sql-connection-alist
      '((local
         (sql-product 'mysql)
         (sql-server "localhost")
         (sql-user "user")
         (sql-password "")
         (sql-database "")
         (sql-port 3306))))

功能:

(defun sql-local ()
  "Connect to the local MySQL server"
  (interactive "sWhat database do you want to connect? ")
  (let ((symbol (intern key)))
    ;here I want to set the database field for sql-database = symbol
     (sql-connect-preset symbol)))

如果我在Perl中做过,那就是这样的:

my %databases = (
    "local" => {
         "sql-product" => "mysql",
         "sql-server" => "localhost",
         "sql-user" => "user",
         "sql-password" => "",
         "sql-database" => "",
         "sql-port" => "3306",
    },
);

$databases{local}->{sql-database} = $symbol;

1 个答案:

答案 0 :(得分:1)

如果您不确定在sql-connection-alist使用中是否找到了所需内容:

(let ((place (assoc 'sql-database (assoc 'local sql-connection-alist))))
  (when (consp place)
    (setcdr place (list symbol))))

否则你也可以使用:

(setcdr (assoc 'sql-database (assoc 'local sql-connection-alist)) (list symbol))