unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* count matches in string, not region?
@ 2020-02-26  4:29 Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26  4:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26  6:24 ` count matches in string, not region? Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-02-26  4:29 UTC (permalink / raw)
  To: help-gnu-emacs

This [last] I wrote a couple of nights ago, it
is part of my ISBN stuff [1] that will soon be
a MELPA package, God willing. But more on that
if and when it happens...

Anyway as you see the 4th line doesn't look
good, instead of moving the point one would
like to invoke the/a count function with the
already-fetched isbn-as argument.

So is there a count-matches-in-string or
something to that extent anywhere, that you are
aware of?

3.1415-out

(defun check-isbn-at-point ()
  (interactive)
  (let*((isbn-string (thing-at-point 'symbol t))
        (num-digits (count-matches "[[:digit:]]" (point) (progn (forward-symbol 1) (point))))
        (check-digit (if (> num-digits 10)
                         (checksum-isbn-13 isbn-string)
                       (checksum-isbn-10 isbn-string) )))
    (message "check digit: %s" check-digit)
    ))
(defalias 'ciap #'check-isbn-at-point)


[1] https://dataswamp.org/~incal/emacs-init/isbn-new.el

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: count matches in string, not region?
  2020-02-26  4:29 count matches in string, not region? Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-02-26  4:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26  5:04   ` `string-to-number' 0 (was: Re: count matches in string, not region?) Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26  6:24 ` count matches in string, not region? Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 1 reply; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-02-26  4:48 UTC (permalink / raw)
  To: help-gnu-emacs

> This [last] I wrote a couple of nights ago,
> it is part of my ISBN stuff [1] that will
> soon be a MELPA package, God willing.
> But more on that if and when it happens...

Wait, that stuff is broke! OK, I'll fix it...
But please respond to the original question
anyway :)

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

* `string-to-number' 0 (was: Re: count matches in string, not region?)
  2020-02-26  4:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-02-26  5:04   ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26  6:13     ` string to digits only (was: Re: `string-to-number' 0 (was: Re: count matches in string, not region?)) Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26  7:37     ` `string-to-number' 0 VanL
  0 siblings, 2 replies; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-02-26  5:04 UTC (permalink / raw)
  To: help-gnu-emacs

>> This [last] I wrote a couple of nights ago,
>> it is part of my ISBN stuff [1] that will
>> soon be a MELPA package, God willing.
>> But more on that if and when it happens...
>
> Wait, that stuff is broke! OK, I'll fix it...
> But please respond to the original question
> anyway :)

Ah, its `thing-at-point', with Biblatex
entries, e.g.

;; @book{russian-arms-transfers-to-east,
;;   author     = {Alexander Sergounin},
;;   isbn       = {0-19-829576-6},
;;   publisher  = {Oxford},
;;   title      = {Russian Arms Transfers to East Asia in the 1990s},
;;   year       = 1999
;; }
;;
;; @book{baa-lo-4,
;;   author     = {Yukito Kishiro},
;;   isbn       = {978-1-61262-294-1},
;;   publisher  = {Kodansha},
;;   title      = {Battle Angel Alita: The Last Order 4},
;;   year       = {2014 (2011)}
;; }

it reads the curly brackets as well! OK,
question to, how do you drop everything from
a string that isn't an integer?

Maybe `cl-remove-if-not' but `string-to-number'
reports 0 both for string number 0 and strings
that are not numbers:

(string-to-number "a") ; 0
(string-to-number "0") ; 0
(string-to-number "1") ; 1

From the docstring: "Return 0 if STRING cannot
be parsed as an integer or floating
point number."

Isn't that a bad idea? How do you know "a" from
the legit 0? Also, why return an integer to
signal DNC, when the purpose is to get
an integer?

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

* string to digits only (was: Re: `string-to-number' 0 (was: Re: count matches in string, not region?))
  2020-02-26  5:04   ` `string-to-number' 0 (was: Re: count matches in string, not region?) Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-02-26  6:13     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26  7:37     ` `string-to-number' 0 VanL
  1 sibling, 0 replies; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-02-26  6:13 UTC (permalink / raw)
  To: help-gnu-emacs

> [...] it reads the curly brackets as well! OK,
> question two, how do you drop everything from
> a string that isn't an integer?

This is perhaps a good way:

  (string-to-number (replace-regexp-in-string "[^0-9]" "" "{978-1-61262-294-1}")) ; 9781612622941

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: count matches in string, not region?
  2020-02-26  4:29 count matches in string, not region? Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26  4:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-02-26  6:24 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26 19:49   ` Tassilo Horn
  1 sibling, 1 reply; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-02-26  6:24 UTC (permalink / raw)
  To: help-gnu-emacs

> Anyway as you see the 4th line doesn't look
> good, instead of moving the point one would
> like to invoke the/a count function with the
> already-fetched isbn-as argument.
>
> So is there a count-matches-in-string or
> something to that extent anywhere, that you
> are aware of?

Problem solved with `replace-regexp-in-string'.

Can still be useful to count matches in
a string, I guess...

Yank it in a temporary buffer and then use
region there is perhaps a poor man's solution...

(defun check-isbn-at-point ()
  "Compute and echo the check digit from the ISBN at point."
  (interactive)
  (let*((isbn-string (thing-at-point 'symbol t))
        (digits-only (replace-regexp-in-string "[^0-9]" "" isbn-string))
        (num-digits  (length digits-only))
        (check-digit (if (> num-digits 10)
                         (checksum-isbn-13 digits-only)
                       (checksum-isbn-10 digits-only) )))
    (message "check digit: %s" check-digit)
    ))
(defalias 'ciap #'check-isbn-at-point)

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `string-to-number' 0
  2020-02-26  5:04   ` `string-to-number' 0 (was: Re: count matches in string, not region?) Emanuel Berg via Users list for the GNU Emacs text editor
  2020-02-26  6:13     ` string to digits only (was: Re: `string-to-number' 0 (was: Re: count matches in string, not region?)) Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-02-26  7:37     ` VanL
  2020-02-26  8:44       ` Emanuel Berg
  1 sibling, 1 reply; 9+ messages in thread
From: VanL @ 2020-02-26  7:37 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: moasenwood


> (string-to-number "a") ; 0
> (string-to-number "0") ; 0
> (string-to-number "1") ; 1
>
> From the docstring: "Return 0 if STRING cannot
> be parsed as an integer or floating
> point number."

(string-to-number "<f1>") ; 60

Should "a" or "<f1>" give nil from string-to-number?



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: `string-to-number' 0
  2020-02-26  7:37     ` `string-to-number' 0 VanL
@ 2020-02-26  8:44       ` Emanuel Berg
  0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg @ 2020-02-26  8:44 UTC (permalink / raw)
  To: VanL; +Cc: help-gnu-emacs

VanL wrote:

>> (string-to-number "a") ; 0
>> (string-to-number "0") ; 0
>> (string-to-number "1") ; 1
>>
>> From the docstring: "Return 0 if STRING
>> cannot be parsed as an integer or floating
>> point number."
>
> (string-to-number "<f1>") ; 60

I get two big 0s:

  (string-to-number "a")    ; 0
  (string-to-number "<f1>") ; 0

The docstring also says "Parse STRING as
a decimal number and return the number."

... but isn't 0 a decimal number?

But come to think of it, it actually DOES work
for 0!

  (string-to-number "0") ; 0

Its just one can't tell that from the bogus
inputs and their outputs, which are 0 as well.
One can just verify the input, I guess.


Meta: I got this message as a mail, and it
seems it didn't show up in <gmane.emacs.help>.
So I suppose I reply to it as a mail as well,
and that defaults to To: header to the previous
poster, and Cc: header to
<help-gnu-emacs@gnu.org>. Keep it uptime. Atem.

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: count matches in string, not region?
  2020-02-26  6:24 ` count matches in string, not region? Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-02-26 19:49   ` Tassilo Horn
  2020-02-26 21:22     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 9+ messages in thread
From: Tassilo Horn @ 2020-02-26 19:49 UTC (permalink / raw)
  To: help-gnu-emacs

Another way would be

  (string-match-p "^[[:digit:]]\\{13\\}$" isbn)

which returns non-nil iff isbn is a string of exactly thirteen digits.

Bye,
Tassilo

Am Mi, 26. Feb 2020, um 07:24, schrieb Emanuel Berg via Users list for the GNU Emacs text editor:
> > Anyway as you see the 4th line doesn't look
> > good, instead of moving the point one would
> > like to invoke the/a count function with the
> > already-fetched isbn-as argument.
> >
> > So is there a count-matches-in-string or
> > something to that extent anywhere, that you
> > are aware of?
> 
> Problem solved with `replace-regexp-in-string'.
> 
> Can still be useful to count matches in
> a string, I guess...
> 
> Yank it in a temporary buffer and then use
> region there is perhaps a poor man's solution...
> 
> (defun check-isbn-at-point ()
>   "Compute and echo the check digit from the ISBN at point."
>   (interactive)
>   (let*((isbn-string (thing-at-point 'symbol t))
>         (digits-only (replace-regexp-in-string "[^0-9]" "" isbn-string))
>         (num-digits  (length digits-only))
>         (check-digit (if (> num-digits 10)
>                          (checksum-isbn-13 digits-only)
>                        (checksum-isbn-10 digits-only) )))
>     (message "check digit: %s" check-digit)
>     ))
> (defalias 'ciap #'check-isbn-at-point)
> 
> -- 
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
> 
> 
> 
>



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: count matches in string, not region?
  2020-02-26 19:49   ` Tassilo Horn
@ 2020-02-26 21:22     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 9+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-02-26 21:22 UTC (permalink / raw)
  To: help-gnu-emacs

Tassilo Horn wrote:

> Another way would be
>
>   (string-match-p "^[[:digit:]]\\{13\\}$" isbn)
>
> which returns non-nil iff isbn is a string of
> exactly thirteen digits.

Okay ... another way of doing ...?

Here is an example of an ISBN-10:

  0-13-308504-X

and here is an ISBN-13:

  978-91-0-011493-0

the check digit is the last one (X meaning 10,
and 0, respectively), so actually one only
needs 9 digits in the first case and 12 in
the second, to compute the check digit.

The groupings (...-...-...-...) do mean things
<https://dataswamp.org/~incal/books/isbn.txt>
but not with respect to how the check digit is
computed, so one can just drop dashes and all
chars that aren't digits.

More examples in the old file,
  https://dataswamp.org/~incal/emacs-init/isbn-verify.el

Thanks for the mail tho, I really want to close
the BOOK on this, so fire away all suggestion
you have :)

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-02-26 21:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-02-26  4:29 count matches in string, not region? Emanuel Berg via Users list for the GNU Emacs text editor
2020-02-26  4:48 ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-02-26  5:04   ` `string-to-number' 0 (was: Re: count matches in string, not region?) Emanuel Berg via Users list for the GNU Emacs text editor
2020-02-26  6:13     ` string to digits only (was: Re: `string-to-number' 0 (was: Re: count matches in string, not region?)) Emanuel Berg via Users list for the GNU Emacs text editor
2020-02-26  7:37     ` `string-to-number' 0 VanL
2020-02-26  8:44       ` Emanuel Berg
2020-02-26  6:24 ` count matches in string, not region? Emanuel Berg via Users list for the GNU Emacs text editor
2020-02-26 19:49   ` Tassilo Horn
2020-02-26 21:22     ` Emanuel Berg via Users list for the GNU Emacs text editor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).