unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Getting substring
@ 2023-08-16  9:26 Heime
  2023-08-16 13:18 ` Heime
  2023-08-19 22:31 ` Emanuel Berg
  0 siblings, 2 replies; 13+ messages in thread
From: Heime @ 2023-08-16  9:26 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor

I have a variable named 'multistr' which may consist of multiple parts separated by spaces.
How can I get the string before the first space in elisp ?

(setq multistr "this that")
(message "%s" str) giving "this" 





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

* Re: Getting substring
  2023-08-16  9:26 Getting substring Heime
@ 2023-08-16 13:18 ` Heime
  2023-08-17  2:23   ` Heime
  2023-08-19 22:31 ` Emanuel Berg
  1 sibling, 1 reply; 13+ messages in thread
From: Heime @ 2023-08-16 13:18 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

------- Original Message -------
On Wednesday, August 16th, 2023 at 9:26 PM, Heime <heimeborgia@protonmail.com> wrote:


> I have a variable named 'multistr' which may consist of multiple parts separated by spaces.
> How can I get the string before the first space in elisp ?
> 
> (setq multistr "this that")
> (message "%s" str) giving "this"

Have used 

  (setq str "bigoplus ⨁︁")
  (message "%s TEST: " (car (split-string str)))
  (setq str "bigoplus︁")
  (message "%s TEST: " (car (split-string str)))

Bet there is something very wrong with it.




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

* Re: Getting substring
  2023-08-16 13:18 ` Heime
@ 2023-08-17  2:23   ` Heime
  2023-08-17  2:30     ` Pierre Rouleau
  0 siblings, 1 reply; 13+ messages in thread
From: Heime @ 2023-08-17  2:23 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

------- Original Message -------
On Thursday, August 17th, 2023 at 1:18 AM, Heime <heimeborgia@protonmail.com> wrote:


> ------- Original Message -------
> On Wednesday, August 16th, 2023 at 9:26 PM, Heime heimeborgia@protonmail.com wrote:
> 
> 
> 
> > I have a variable named 'multistr' which may consist of multiple parts separated by spaces.
> > How can I get the string before the first space in elisp ?
> > 
> > (setq multistr "this that")
> > (message "%s" str) giving "this"
> 
> 
> Have used
> 
> (setq str "bigoplus ⨁︁")
> (message "%s TEST: " (car (split-string str)))
> (setq str "bigoplus︁")
> (message "%s TEST: " (car (split-string str)))
> 
> But there is something very wrong with it.

Why does the following not print the first word?

(message "%s TEST: " (car (split-string grafm)))




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

* Re: Getting substring
  2023-08-17  2:23   ` Heime
@ 2023-08-17  2:30     ` Pierre Rouleau
  2023-08-17  2:33       ` Pierre Rouleau
  0 siblings, 1 reply; 13+ messages in thread
From: Pierre Rouleau @ 2023-08-17  2:30 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

> Why does the following not print the first word?
>
> (message "%s TEST: " (car (split-string grafm)))
>

Are you sure it does not print it?  It prints bigoplus  for me.
You can also look into the '*Message*' buffer to see what was printed by
the message function.


-- 
/Pierre


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

* Re: Getting substring
  2023-08-17  2:30     ` Pierre Rouleau
@ 2023-08-17  2:33       ` Pierre Rouleau
  2023-08-17  2:52         ` Heime
  0 siblings, 1 reply; 13+ messages in thread
From: Pierre Rouleau @ 2023-08-17  2:33 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

I mean,
 Executing :
> (setq str "bigoplus ⨁︁")
> (message "%s TEST: " (car (split-string str)))

prints bigoplus.

but

(message "%s TEST: " (car (split-string grafm)))

will depend of what grafm holds.

On Wed, Aug 16, 2023 at 10:30 PM Pierre Rouleau <prouleau001@gmail.com>
wrote:

>
> Why does the following not print the first word?
>>
>> (message "%s TEST: " (car (split-string grafm)))
>>
>
> Are you sure it does not print it?  It prints bigoplus  for me.
> You can also look into the '*Message*' buffer to see what was printed by
> the message function.
>
>
> --
> /Pierre
>


-- 
/Pierre


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

* Re: Getting substring
  2023-08-17  2:33       ` Pierre Rouleau
@ 2023-08-17  2:52         ` Heime
  2023-08-17  3:01           ` Heime
  2023-08-17  3:01           ` Pierre Rouleau
  0 siblings, 2 replies; 13+ messages in thread
From: Heime @ 2023-08-17  2:52 UTC (permalink / raw)
  To: Pierre Rouleau; +Cc: Heime via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

------- Original Message -------
On Thursday, August 17th, 2023 at 2:33 PM, Pierre Rouleau <prouleau001@gmail.com> wrote:


> I mean,
> Executing :
> 
> > (setq str "bigoplus ⨁︁")
> > (message "%s TEST: " (car (split-string str)))
> 
> 
> prints bigoplus.
> 
> but
> 
> (message "%s TEST: " (car (split-string grafm)))
> 
> will depend of what grafm holds.
> 
> On Wed, Aug 16, 2023 at 10:30 PM Pierre Rouleau prouleau001@gmail.com
> 
> wrote:
> 
> > Why does the following not print the first word?
> > 
> > > (message "%s TEST: " (car (split-string grafm)))
> > 
> > Are you sure it does not print it? It prints bigoplus for me.
> > You can also look into the 'Message' buffer to see what was printed by
> > the message function. - Pierre

I now realise my mistake of putting "%s" before "TEST".  I was thus doing things
correctly after all.

The command (car (split-string grafm)) also works if there are no spaces in
'grafm', it still returns the string as a first element in the list result.





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

* Re: Getting substring
  2023-08-17  2:52         ` Heime
@ 2023-08-17  3:01           ` Heime
  2023-08-17  3:07             ` Pierre Rouleau
  2023-08-17  3:01           ` Pierre Rouleau
  1 sibling, 1 reply; 13+ messages in thread
From: Heime @ 2023-08-17  3:01 UTC (permalink / raw)
  To: Heime; +Cc: Pierre Rouleau,
	Heime via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

------- Original Message -------
On Thursday, August 17th, 2023 at 2:52 PM, Heime <heimeborgia@protonmail.com> wrote:


> 
> 
> 
> 
> 
> Sent with Proton Mail secure email.
> 
> 
> ------- Original Message -------
> On Thursday, August 17th, 2023 at 2:33 PM, Pierre Rouleau prouleau001@gmail.com wrote:
> 
> 
> 
> > I mean,
> > Executing :
> > 
> > > (setq str "bigoplus ⨁︁")
> > > (message "%s TEST: " (car (split-string str)))
> > 
> > prints bigoplus.
> > 
> > but
> > 
> > (message "%s TEST: " (car (split-string grafm)))
> > 
> > will depend of what grafm holds.
> > 
> > On Wed, Aug 16, 2023 at 10:30 PM Pierre Rouleau prouleau001@gmail.com
> > 
> > wrote:
> > 
> > > Why does the following not print the first word?
> > > 
> > > > (message "%s TEST: " (car (split-string grafm)))
> > > 
> > > Are you sure it does not print it? It prints bigoplus for me.
> > > You can also look into the 'Message' buffer to see what was printed by
> > > the message function. - Pierre
> 
> 
> I now realise my mistake of putting "%s" before "TEST". I was thus doing things
> correctly after all.
> 
> The command (car (split-string grafm)) also works if there are no spaces in
> 'grafm', it still returns the string as a first element in the list result.

Have started to wonder whether the following is wrong to do

(setq grafm "bigoplus ⨁︁")
(pcase (car (split-string grafm))
  "bigoplus" ("Detected bigoplus")) 

It is not a string.




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

* Re: Getting substring
  2023-08-17  2:52         ` Heime
  2023-08-17  3:01           ` Heime
@ 2023-08-17  3:01           ` Pierre Rouleau
  1 sibling, 0 replies; 13+ messages in thread
From: Pierre Rouleau @ 2023-08-17  3:01 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

> > Why does the following not print the first word?
> > >
> > > > (message "%s TEST: " (car (split-string grafm)))
> > >
> > > Are you sure it does not print it? It prints bigoplus for me.
> > > You can also look into the 'Message' buffer to see what was printed by
> > > the message function. - Pierre
>
> I now realise my mistake of putting "%s" before "TEST".  I was thus doing
> things
> correctly after all.
>
> The command (car (split-string grafm)) also works if there are no spaces in
> 'grafm', it still returns the string as a first element in the list result.
>
> Not sure I follow you...
- You do understand that message is just a string formatting function that
takes arguments to put in places identified by the '%s', right?
- And that format does something similar but just returns the formatted
string.
- therefore you can use ielm to test your code (use it!):

Here's a session:

ELISP> (setq str "bigoplus ⨁︁")
"bigoplus ⨁︁"
ELISP> (car (split-string str))
"bigoplus"
ELISP> (format " 123-- %s -- " (car (split-string str)))
" 123-- bigoplus -- "
ELISP> (format "%s TEST" (car (split-string str)))
"bigoplus TEST"
ELISP> (format "Survey says: %s " (car (split-string str)))
"Survey says: bigoplus "
ELISP>









-- 
/Pierre


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

* Re: Getting substring
  2023-08-17  3:01           ` Heime
@ 2023-08-17  3:07             ` Pierre Rouleau
  2023-08-17  3:16               ` Heime
  0 siblings, 1 reply; 13+ messages in thread
From: Pierre Rouleau @ 2023-08-17  3:07 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

Heime,

I think you need to learn a little bit of Emacs Lisp, or Lisp.  Read a book
on Lisp to get you going.   Asking questions like you do won't help you
much.  You first need to understand LIsp and the way it deals with
functions, the concepts.

LISPcraft from Robert Wilensky is old (1984) but small and it covers the
basics which apply to Emacs Lisp.    It's a nice read and the book is
relatively small.


On Wed, Aug 16, 2023 at 11:01 PM Heime <heimeborgia@protonmail.com> wrote:

>
>
>
>
>
> Sent with Proton Mail secure email.
>
> ------- Original Message -------
> On Thursday, August 17th, 2023 at 2:52 PM, Heime <
> heimeborgia@protonmail.com> wrote:
>
>
> >
> >
> >
> >
> >
> > Sent with Proton Mail secure email.
> >
> >
> > ------- Original Message -------
> > On Thursday, August 17th, 2023 at 2:33 PM, Pierre Rouleau
> prouleau001@gmail.com wrote:
> >
> >
> >
> > > I mean,
> > > Executing :
> > >
> > > > (setq str "bigoplus ⨁︁")
> > > > (message "%s TEST: " (car (split-string str)))
> > >
> > > prints bigoplus.
> > >
> > > but
> > >
> > > (message "%s TEST: " (car (split-string grafm)))
> > >
> > > will depend of what grafm holds.
> > >
> > > On Wed, Aug 16, 2023 at 10:30 PM Pierre Rouleau prouleau001@gmail.com
> > >
> > > wrote:
> > >
> > > > Why does the following not print the first word?
> > > >
> > > > > (message "%s TEST: " (car (split-string grafm)))
> > > >
> > > > Are you sure it does not print it? It prints bigoplus for me.
> > > > You can also look into the 'Message' buffer to see what was printed
> by
> > > > the message function. - Pierre
> >
> >
> > I now realise my mistake of putting "%s" before "TEST". I was thus doing
> things
> > correctly after all.
> >
> > The command (car (split-string grafm)) also works if there are no spaces
> in
> > 'grafm', it still returns the string as a first element in the list
> result.
>
> Have started to wonder whether the following is wrong to do
>
> (setq grafm "bigoplus ⨁︁")
> (pcase (car (split-string grafm))
>   "bigoplus" ("Detected bigoplus"))
>
> It is not a string.
>
>

-- 
/Pierre


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

* Re: Getting substring
  2023-08-17  3:07             ` Pierre Rouleau
@ 2023-08-17  3:16               ` Heime
  2023-08-17 12:37                 ` Pierre Rouleau
  0 siblings, 1 reply; 13+ messages in thread
From: Heime @ 2023-08-17  3:16 UTC (permalink / raw)
  To: Pierre Rouleau; +Cc: Heime via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

------- Original Message -------
On Thursday, August 17th, 2023 at 3:07 PM, Pierre Rouleau <prouleau001@gmail.com> wrote:


> Heime,
> 
> I think you need to learn a little bit of Emacs Lisp, or Lisp. Read a book
> on Lisp to get you going. Asking questions like you do won't help you
> much. You first need to understand LIsp and the way it deals with
> functions, the concepts.
> 
> LISPcraft from Robert Wilensky is old (1984) but small and it covers the
> basics which apply to Emacs Lisp. It's a nice read and the book is
> relatively small.
 
The error message (wrong-type-argument listp "bigoplus"), suggests that the code 
expects a list argument but is receiving a string instead.

Doing C-h f pcase says

(pcase EXP &rest CASES)

Evaluate EXP to get EXPVAL; try passing control to one of CASES.

Does not specify whether EXP is a string or whatever.

> On Wed, Aug 16, 2023 at 11:01 PM Heime heimeborgia@protonmail.com wrote:
> 
> > Sent with Proton Mail secure email.
> > 
> > ------- Original Message -------
> > On Thursday, August 17th, 2023 at 2:52 PM, Heime <
> > heimeborgia@protonmail.com> wrote:
> > 
> > > Sent with Proton Mail secure email.
> > > 
> > > ------- Original Message -------
> > > On Thursday, August 17th, 2023 at 2:33 PM, Pierre Rouleau
> > > prouleau001@gmail.com wrote:
> > > 
> > > > I mean,
> > > > Executing :
> > > > 
> > > > > (setq str "bigoplus ⨁︁")
> > > > > (message "%s TEST: " (car (split-string str)))
> > > > 
> > > > prints bigoplus.
> > > > 
> > > > but
> > > > 
> > > > (message "%s TEST: " (car (split-string grafm)))
> > > > 
> > > > will depend of what grafm holds.
> > > > 
> > > > On Wed, Aug 16, 2023 at 10:30 PM Pierre Rouleau prouleau001@gmail.com
> > > > 
> > > > wrote:
> > > > 
> > > > > Why does the following not print the first word?
> > > > > 
> > > > > > (message "%s TEST: " (car (split-string grafm)))
> > > > > 
> > > > > Are you sure it does not print it? It prints bigoplus for me.
> > > > > You can also look into the 'Message' buffer to see what was printed
> > > > > by
> > > > > the message function. - Pierre
> > > 
> > > I now realise my mistake of putting "%s" before "TEST". I was thus doing
> > > things
> > > correctly after all.
> > > 
> > > The command (car (split-string grafm)) also works if there are no spaces
> > > in
> > > 'grafm', it still returns the string as a first element in the list
> > > result.
> > 
> > Have started to wonder whether the following is wrong to do
> > 
> > (setq grafm "bigoplus ⨁︁")
> > (pcase (car (split-string grafm))
> > "bigoplus" ("Detected bigoplus"))
> > 
> > It is not a string.
> 
> 
> --
> /Pierre



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

* Re: Getting substring
  2023-08-17  3:16               ` Heime
@ 2023-08-17 12:37                 ` Pierre Rouleau
  0 siblings, 0 replies; 13+ messages in thread
From: Pierre Rouleau @ 2023-08-17 12:37 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

Heime,

reading the following might help
- https://www.gnu.org/software/emacs/manual/html_node/elisp/pcase-Macro.html
- https://www.emacswiki.org/emacs/PatternMatching

On Wed, Aug 16, 2023 at 11:16 PM Heime <heimeborgia@protonmail.com> wrote:

>
>
>
>
>
> Sent with Proton Mail secure email.
>
> ------- Original Message -------
> On Thursday, August 17th, 2023 at 3:07 PM, Pierre Rouleau <
> prouleau001@gmail.com> wrote:
>
>
> > Heime,
> >
> > I think you need to learn a little bit of Emacs Lisp, or Lisp. Read a
> book
> > on Lisp to get you going. Asking questions like you do won't help you
> > much. You first need to understand LIsp and the way it deals with
> > functions, the concepts.
> >
> > LISPcraft from Robert Wilensky is old (1984) but small and it covers the
> > basics which apply to Emacs Lisp. It's a nice read and the book is
> > relatively small.
>
> The error message (wrong-type-argument listp "bigoplus"), suggests that
> the code
> expects a list argument but is receiving a string instead.
>
> Doing C-h f pcase says
>
> (pcase EXP &rest CASES)
>
> Evaluate EXP to get EXPVAL; try passing control to one of CASES.
>
> Does not specify whether EXP is a string or whatever.
>
> > On Wed, Aug 16, 2023 at 11:01 PM Heime heimeborgia@protonmail.com wrote:
> >
> > > Sent with Proton Mail secure email.
> > >
> > > ------- Original Message -------
> > > On Thursday, August 17th, 2023 at 2:52 PM, Heime <
> > > heimeborgia@protonmail.com> wrote:
> > >
> > > > Sent with Proton Mail secure email.
> > > >
> > > > ------- Original Message -------
> > > > On Thursday, August 17th, 2023 at 2:33 PM, Pierre Rouleau
> > > > prouleau001@gmail.com wrote:
> > > >
> > > > > I mean,
> > > > > Executing :
> > > > >
> > > > > > (setq str "bigoplus ⨁︁")
> > > > > > (message "%s TEST: " (car (split-string str)))
> > > > >
> > > > > prints bigoplus.
> > > > >
> > > > > but
> > > > >
> > > > > (message "%s TEST: " (car (split-string grafm)))
> > > > >
> > > > > will depend of what grafm holds.
> > > > >
> > > > > On Wed, Aug 16, 2023 at 10:30 PM Pierre Rouleau
> prouleau001@gmail.com
> > > > >
> > > > > wrote:
> > > > >
> > > > > > Why does the following not print the first word?
> > > > > >
> > > > > > > (message "%s TEST: " (car (split-string grafm)))
> > > > > >
> > > > > > Are you sure it does not print it? It prints bigoplus for me.
> > > > > > You can also look into the 'Message' buffer to see what was
> printed
> > > > > > by
> > > > > > the message function. - Pierre
> > > >
> > > > I now realise my mistake of putting "%s" before "TEST". I was thus
> doing
> > > > things
> > > > correctly after all.
> > > >
> > > > The command (car (split-string grafm)) also works if there are no
> spaces
> > > > in
> > > > 'grafm', it still returns the string as a first element in the list
> > > > result.
> > >
> > > Have started to wonder whether the following is wrong to do
> > >
> > > (setq grafm "bigoplus ⨁︁")
> > > (pcase (car (split-string grafm))
> > > "bigoplus" ("Detected bigoplus"))
> > >
> > > It is not a string.
> >
> >
> > --
> > /Pierre
>


-- 
/Pierre


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

* Re: Getting substring
  2023-08-16  9:26 Getting substring Heime
  2023-08-16 13:18 ` Heime
@ 2023-08-19 22:31 ` Emanuel Berg
  2023-08-25  7:39   ` Philip Kaludercic
  1 sibling, 1 reply; 13+ messages in thread
From: Emanuel Berg @ 2023-08-19 22:31 UTC (permalink / raw)
  To: help-gnu-emacs

Heime wrote:

> How can I get the string before the first space in elisp ?
>
> (setq multistr "this that")

(car (split-string "first string")) ; "first"

See the docstring for `split-string'.

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Getting substring
  2023-08-19 22:31 ` Emanuel Berg
@ 2023-08-25  7:39   ` Philip Kaludercic
  0 siblings, 0 replies; 13+ messages in thread
From: Philip Kaludercic @ 2023-08-25  7:39 UTC (permalink / raw)
  To: help-gnu-emacs

Emanuel Berg <incal@dataswamp.org> writes:

> Heime wrote:
>
>> How can I get the string before the first space in elisp ?
>>
>> (setq multistr "this that")
>
> (car (split-string "first string")) ; "first"
>
> See the docstring for `split-string'.

Another idea is to use regular expressions and their match data:

--8<---------------cut here---------------start------------->8---
(let ((str "this that"))
  (and (string-match "\\([^[:space:]]+\\)[[:space:]]?" str)
       (match-string 1 str)))
--8<---------------cut here---------------end--------------->8---



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

end of thread, other threads:[~2023-08-25  7:39 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-16  9:26 Getting substring Heime
2023-08-16 13:18 ` Heime
2023-08-17  2:23   ` Heime
2023-08-17  2:30     ` Pierre Rouleau
2023-08-17  2:33       ` Pierre Rouleau
2023-08-17  2:52         ` Heime
2023-08-17  3:01           ` Heime
2023-08-17  3:07             ` Pierre Rouleau
2023-08-17  3:16               ` Heime
2023-08-17 12:37                 ` Pierre Rouleau
2023-08-17  3:01           ` Pierre Rouleau
2023-08-19 22:31 ` Emanuel Berg
2023-08-25  7:39   ` Philip Kaludercic

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).