Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 40 |
Nodes: | 6 (0 / 6) |
Uptime: | 11:18:30 |
Calls: | 291 |
Files: | 910 |
Messages: | 76,440 |
As a concrete example, suppose (as I did in a program I recently
wrote) that you have a string with several arbitrary variable names
you need to substitute in, and a hash with their values. This can be
done in one line in Perl:
# $string holds something like:
# 'The $dimension of the $obj is $length cm.'
# %vars holds something like:
# { $dimension => length, $obj => plank, $length => 105 }
$string =~ s/\$(\S+)/$vars->{$1}/g;
Now, look at a Lisp parallel. You have a list, and you want to
substitute keywords with their values from an alist:
;; LIST holds something like:
;; (the :dimension of the :object is :value cm)
;; VARS holds something like:
;; ((:dimension . length) (:object . plank) (:value . 105))
(mapcar #'(lambda (elt)
(if (keywordp elt)
(cdr (assoc elt vars))
elt))
list)