1
0
Fork 0
cl-sites/novaspec.org/cl/f_gethash.html
2025-02-05 18:52:26 +01:00

427 lines
No EOL
8.2 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>gethash | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_gethash.html"
><LINK REL="next" HREF="f_remhash.html" TYPE="text/html" TITLE="remhash"
><LINK REL="prev" HREF="f_hash-table-test.html" TYPE="text/html" TITLE="hash-table-test"
><LINK REL="up" HREF="18_2_Hash_Tables_Dictionary.html" TYPE="text/html" TITLE="18.2 Hash Tables Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="18_Hash_Tables.html"
>18. Hash Tables</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="18_2_Hash_Tables_Dictionary.html"
>18.2 Hash Tables Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_gethash.html"
>gethash</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_hash-table-test.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="f_remhash.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="gethash"
></SPAN
><B
>gethash</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Accessor</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
><B
>gethash</B
> <SPAN CLASS="cmssi"
>key</SPAN
> <SPAN CLASS="cmssi"
>hash-table</SPAN
> <SPAN CLASS="cmtt"
>&amp;optional</SPAN
> <SPAN CLASS="cmssi"
>default</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>value,</SPAN
> <SPAN CLASS="cmssi"
>present-p</SPAN
></P
><P CLASS="j"
><B
>(setf (gethash</B
> <SPAN CLASS="cmssi"
>key</SPAN
> <SPAN CLASS="cmssi"
>hash-table</SPAN
> <SPAN CLASS="cmtt"
>&amp;optional</SPAN
> <SPAN CLASS="cmssi"
>default</SPAN
><B
>)</B
> <SPAN CLASS="cmssi"
>new-value</SPAN
><B
>)</B
></P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>key</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>hash-table</VAR
> &#8212; a <A HREF="26_1_Glossary.html#hash_table"
><EM CLASS="term"
>hash table</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>default</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. The default is <SPAN CLASS="misc"
><B
>nil</B
></SPAN
>. </P
><P CLASS="j"
><VAR CLASS="param"
>value</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>present-p</VAR
> &#8212; a <A HREF="26_1_Glossary.html#generalized_boolean"
><EM CLASS="term"
>generalized boolean</EM
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>Value</VAR
> is the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> in <VAR CLASS="param"
>hash-table</VAR
> whose <A HREF="26_1_Glossary.html#key"
><EM CLASS="term"
>key</EM
></A
> is the <A HREF="26_1_Glossary.html#same"
><EM CLASS="term"
>same</EM
></A
> as <VAR CLASS="param"
>key</VAR
> under the <VAR CLASS="param"
>hash-table</VAR
>&#8217;s equivalence test. If there is no such entry, <VAR CLASS="param"
>value</VAR
> is the <VAR CLASS="param"
>default</VAR
>. </P
><P CLASS="j"
><VAR CLASS="param"
>Present-p</VAR
> is <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
> if an entry is found; otherwise, it is <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>. </P
><P CLASS="j"
><A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> may be used with <A HREF="f_gethash.html" CLASS="funref"
><B
>gethash</B
></A
> to modify the <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>value</EM
></A
> associated with a given <A HREF="26_1_Glossary.html#key"
><EM CLASS="term"
>key</EM
></A
>, or to add a new entry. When a <A HREF="f_gethash.html" CLASS="funref"
><B
>gethash</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> is used as a <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> <VAR CLASS="param"
>place</VAR
>, any <VAR CLASS="param"
>default</VAR
> which is supplied is evaluated according to normal left-to-right evaluation rules, but its <A HREF="26_1_Glossary.html#value"
><EM CLASS="term"
>value</EM
></A
> is ignored.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
>(setq table (make-hash-table)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> #&lt;HASH-TABLE EQL 0/120 32206334&gt;
(gethash 1 table) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL, <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>
(gethash 1 table 2) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 2, <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>
(setf (gethash 1 table) "one") <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> "one"
(setf (gethash 2 table "two") "two") <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> "two"
(gethash 1 table) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> "one", <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>
(gethash 2 table) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> "two", <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>
(gethash nil table) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL, <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>
(setf (gethash nil table) nil) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
(gethash nil table) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL, <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>
(defvar *counters* (make-hash-table)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> *COUNTERS*
(gethash 'foo *counters*) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL, <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>
(gethash 'foo *counters* 0) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 0, <A HREF="26_1_Glossary.html#false"
><EM CLASS="term"
>false</EM
></A
>
(defmacro how-many (obj) `(values (gethash ,obj *counters* 0))) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> HOW-MANY
(defun count-it (obj) (incf (how-many obj))) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> COUNT-IT
(dolist (x '(bar foo foo bar bar baz)) (count-it x))
(how-many 'foo) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 2
(how-many 'bar) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3
(how-many 'quux) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 0</PRE
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_remhash.html" CLASS="funref"
><B
>remhash</B
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#secondary_value"
><EM CLASS="term"
>secondary value</EM
></A
>, <VAR CLASS="param"
>present-p</VAR
>, can be used to distinguish the absence of an entry from the presence of an entry that has a value of <VAR CLASS="param"
>default</VAR
>.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_hash-table-test.html" CLASS="prev"
>&#8592;</A
><A HREF="f_remhash.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>