• Re: alist keys: strings or symbols

    From Steve G@Sgonedes1977@gmail.com to gnu.emacs.help on Wed Mar 30 16:01:10 2022
    From Newsgroup: gnu.emacs.help

    excalamus@tutanota.com writes:

    Some questions about alists:
    - Is it a better practice to convert string keys to symbols?

    I would recommend keeping the data the way it is. Strings are great;
    emacs has hash tables that will work well for strings.

    -a Is =intern= best for this?-a

    No. read-from-string will do better. You don't want numbers and commas
    in your symbols.

    What about handling illegal symbol names?

    the function make-symbol will handle this for you.

    - If a symbol is used as a key and that symbol is already in use
    -a elsewhere, is there potential for conflict with the existing symbol?

    Oh yes. In common lisp the convention for global variables is to use `*'
    around the symbol; such as *gensym-counter*. In emacs lisp, it is
    considered bad form to use such constraints because it messes up the minibuffer, etc.

    I have an alist created from parsing meta data from a file.-a The file
    looks like:

    #+begin_src emacs-lisp :results verbatim :session exc

    [ ... ]

    I do not know why you are using the hash `#'?

    (defvar exc-post-meta-data
    -a (concat
    -a-a "#+TITLE: Test post\n"
    -a-a "#+AUTHOR: Excalamus\n"
    -a-a "#+DATE: 2020-07-17\n"
    -a-a "#+TAGS: blogging tests\n"
    -a-a "\n")
    -a "Sample post meta information.")

    You could do.

    (defvar executable-find '((:TITLE . "Test Post") (:AUTHOR . "Name")))

    This works, but seems like a smell.-a All these problems go
    back to strings as keys.-a Maybe there's a better way?

    I could convert the keys to symbols using =intern=.-a

    I would try hash tables for strings. see below.


    #+RESULTS:
    : ((TITLE . "Test post") (AUTHOR . "Excalamus") (DATE . "2020-07-17") (TAGS . "blogging tests"))

    This has several apparent problems.

    As I understand it, this would pollute the global obarray. Is that a
    real concern?-a I know the symbol is only being used as a lookup;

    Yes it is.

    the variable, function, and properties shouldn't change.-a

    This is the key. You seem to understand it. In emacs lisp there are not duplicate symbols. Two symbols are compared by their address (or
    pointer) which is called `EQ' meaning that they are the same thing.

    if you are re-using symbols; i.e., if the symbols are finite then
    using them should not be a problem. Such as in your case; TITLE, AUTHOR,
    these are good for symbols. Parsing an email with each word as a symbol
    can cause serious problems with the obarray.

    However you could intern the symbols into a different obarray.

    The biggest difference will be at run time. You have to make a hashtable
    at runtime (to be simple about it). With an alist of symbols emacs will
    already place the symbols into it's obarray without the extra code.

    Regardless, I
    don't want my package to conflict with (i.e. overwrite) a person's environment unknowingly.

    In common lisp their are namespaces; but the problem is the same.
    partitioning code is not my specialty.

    The string may also have characters illegal for use as a symbol.-a
    Here's what happens with illegal symbol characters in the string.

    #+begin_src emacs-lisp :results verbatim :session exc
    (setq exc-bad-meta-data
    -a (concat
    -a-a "#+THE TITLE: Test post\n"
    -a-a "#+AUTHOR: Excalamus\n"
    -a-a "#+DATE: 2020-07-17\n"
    -a-a "#+POST TAGS: blogging tests\n"
    -a-a "\n"))

    (setq exc-alist-i-bad (exc-parse-org-meta-data-intern exc-bad-meta-data)) exc-alist-i-bad
    #+end_src

    #+RESULTS:
    : ((AUTHOR . "Excalamus") (DATE . "2020-07-17"))

    How are situations like these best handled?


    I usually use sed, awk, or perl for the input. I write shell script to
    create a simple A-LIST or a file with lines of strings.

    I wish there was a way to write a hashtable to output.
    --- Synchronet 3.21d-Linux NewsLink 1.2