help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help with elisp


From: lawrence mitchell
Subject: Re: Help with elisp
Date: Thu, 31 Jul 2003 12:30:58 +0100
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3.50

David Chadd wrote:

> Apologies for a question demonstrating beginner's total Elisp
> incompetence.  I want to make lists out of series of
> attribute-values in an xml file.  (The values are sigla of
> manuscripts.)  These are e.g. in the form:

>   wit="CAO-C Alb2 Hyd"

> Having found the string of values (CAO-C Alb2 Hyd) with a regexp
> search, I simple-mindedly thought I would be able to do something like

>   (setq wits (split-string (match-string 1)))

> This does indeed make a list --- (listp wits) returns T --- but the
> lists don't behave as I would expect.  For instance, they don't
> respond correctly to (set-difference), (intersection) etc.  And for
> reasons I can guess at, but don't know enough to do anything about,
> the lists are in the form ("CAO-C" "Alb2" "Hyd") rather than (CAO-C
> Alb2 Hyd).

> I'm obviously going totally the wrong way about this.  How should I be
> trying to make a conventional Lisp list out of a found string of this
> kind?  Advice and guidance gratefully received.

In fact, you're almost there.  The only thing you're missing is
the correct equality test.  You see, strings are not EQ, or EQL
in emacs lisp.

(eq "foo" "foo")
   => nil

(eql "foo" "foo")
   => nil

instead, you need to check for equality either with EQUAL, or,
if you know you are using strings, STRING=.

(equal "foo" "foo")
   => t

(string= "foo" "foo")
   => t

Now, the functions SET-DIFFERENCE and INTERSECTION by default
test for equality with EQ, so you need to tell them to use a
different testing function.  This can be done by supplying them
with a keyword argument.

(setq foo '("foo" "bar" "baz")
      bar '("bar" "baz" "quux"))

foo
   => ("foo" "bar" "baz")

bar
   => ("bar" "baz" "quux")

;; Default, comparison done with EQ.
(set-difference foo bar)
   => ("baz" "bar" "foo")

(intersection foo bar)
   => nil

;; Comparison done with STRING=
(set-difference foo bar :test 'string=)
   => ("foo")

(intersection foo bar :test 'string=)
   => ("baz" "bar")

Hope that clears things up a bit
-- 
lawrence mitchell <wence@gmx.li>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]