unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Creating string from list of strings
@ 2002-10-14  6:26 Bill Wohler
  2002-10-14  7:00 ` Stefan Monnier <foo@acm.com>
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Bill Wohler @ 2002-10-14  6:26 UTC (permalink / raw)


  I wasn't able to figure out from the Elisp manual how to turn a list
  of strings into a single string. For example, how does one
  print the following list of strings?

    (setq list-of-strings '("foo" "bar"))
    (message (perform-magic-with list-of-strings))

  While the following works, it seems that there should be a one-liner
  to do this already:

    (defvar my-string)
    (while list-of-strings
      (setq my-string (concat my-string (car list-of-strings)))
      (setq list-of-strings (cdr list-of-strings)))
    (message (my-string))

-- 
Bill Wohler <wohler@newt.com>  http://www.newt.com/wohler/  GnuPG ID:610BD9AD
Maintainer of comp.mail.mh FAQ and mh-e. Vote Libertarian!
If you're passed on the right, you're in the wrong lane.

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

* Re: Creating string from list of strings
  2002-10-14  6:26 Creating string from list of strings Bill Wohler
@ 2002-10-14  7:00 ` Stefan Monnier <foo@acm.com>
  2002-10-14  7:05 ` John Paul Wallington
  2002-10-14 14:00 ` Joe Casadonte
  2 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2002-10-14  7:00 UTC (permalink / raw)


>>>>> "Bill" == Bill Wohler <wohler@newt.com> writes:
>     (message (perform-magic-with list-of-strings))

Beware! `message' is like `printf' in C: it expects a format argument.
So you'd better use (message "%s" (perform-magic-with list-of-strings))

>   While the following works, it seems that there should be a one-liner
>   to do this already:

>     (defvar my-string)
>     (while list-of-strings
>       (setq my-string (concat my-string (car list-of-strings)))
>       (setq list-of-strings (cdr list-of-strings)))
>     (message (my-string))

Try `mapconcat'.


        Stefan

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

* Re: Creating string from list of strings
  2002-10-14  6:26 Creating string from list of strings Bill Wohler
  2002-10-14  7:00 ` Stefan Monnier <foo@acm.com>
@ 2002-10-14  7:05 ` John Paul Wallington
  2002-10-14 14:00 ` Joe Casadonte
  2 siblings, 0 replies; 8+ messages in thread
From: John Paul Wallington @ 2002-10-14  7:05 UTC (permalink / raw)


Bill Wohler <wohler@newt.com> wrote:

>   I wasn't able to figure out from the Elisp manual how to turn a list
>   of strings into a single string. For example, how does one
>   print the following list of strings?
>
>     (setq list-of-strings '("foo" "bar"))
>     (message (perform-magic-with list-of-strings))

How about (message (apply 'concat list-of-strings)) ?

-- 
John Paul Wallington

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

* Re: Creating string from list of strings
  2002-10-14  6:26 Creating string from list of strings Bill Wohler
  2002-10-14  7:00 ` Stefan Monnier <foo@acm.com>
  2002-10-14  7:05 ` John Paul Wallington
@ 2002-10-14 14:00 ` Joe Casadonte
  2002-10-14 15:15   ` Tom Capey
  2002-10-14 15:35   ` Stefan Monnier <foo@acm.com>
  2 siblings, 2 replies; 8+ messages in thread
From: Joe Casadonte @ 2002-10-14 14:00 UTC (permalink / raw)


On Mon, 14 Oct 2002, Bill Wohler wrote:

>   I wasn't able to figure out from the Elisp manual how to turn a
>   list of strings into a single string. For example, how does one
>   print the following list of strings?
>
>     (setq list-of-strings '("foo" "bar"))
>     (message (perform-magic-with list-of-strings))
>
>   While the following works, it seems that there should be a
>   one-liner to do this already:
>
>     (defvar my-string)
>     (while list-of-strings
>       (setq my-string (concat my-string (car list-of-strings)))
>       (setq list-of-strings (cdr list-of-strings)))
>     (message (my-string))

Some folks have already showed you what to do to get a simple concat
like you had asked for.  Here's something I whipped up to get a
perl-like join command.

(defun my-perl-join (join-string elements)
  "[Internal] Function to mimic Perl's join() function.

JOIN-STRING is the string to use to join them together.
ELEMENTS is a list of what to join.

Example:

  \(my-perl-join \"/\" (list \"some\" \"file\" \"path\")

would result in the string:

  some/file/path

Note that no leading slash is put in; JOIN-STRING is only put in
between the joined elements."
  (let (rc active subsequent-pass)
	(while elements
	  (setq active (car elements))
	  (setq elements (cdr elements))

 	  (if (not subsequent-pass)
 		  (setq subsequent-pass t)
		(setq rc (concat rc join-string))
		)

	  (setq rc (concat rc active)))
	rc))


--
Regards,

joe
Joe Casadonte
jcasadonte@northbound-train.com

------------------------------------------------------------------------------
         Llama Fresh Farms => http://www.northbound-train.com
   Gay Media Resource List => http://www.northbound-train.com/gaymedia.html
            Perl for Win32 => http://www.northbound-train.com/perlwin32.html
               Emacs Stuff => http://www.northbound-train.com/emacs.html
          Music CD Trading => http://www.northbound-train.com/cdr.html
------------------------------------------------------------------------------
                       Live Free, that's the message!
------------------------------------------------------------------------------

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

* Re: Creating string from list of strings
  2002-10-14 14:00 ` Joe Casadonte
@ 2002-10-14 15:15   ` Tom Capey
  2002-10-14 15:35   ` Stefan Monnier <foo@acm.com>
  1 sibling, 0 replies; 8+ messages in thread
From: Tom Capey @ 2002-10-14 15:15 UTC (permalink / raw)


* Joe Casadonte <jcasadonte@northbound-train.com> writes:

>On Mon, 14 Oct 2002, Bill Wohler wrote:
> [question on concatenating list strings elided]

> Some folks have already showed you what to do to get a simple concat
> like you had asked for.  Here's something I whipped up to get a
> perl-like join command.
> 
> (defun my-perl-join (join-string elements)
>   "[Internal] Function to mimic Perl's join() function.
> 
> JOIN-STRING is the string to use to join them together.
> ELEMENTS is a list of what to join.
> 
> Example:
> 
>   \(my-perl-join \"/\" (list \"some\" \"file\" \"path\")
> 
> would result in the string:
> 
>   some/file/path
> 
> Note that no leading slash is put in; JOIN-STRING is only put in
> between the joined elements."
>   (let (rc active subsequent-pass)
> 	(while elements
> 	  (setq active (car elements))
> 	  (setq elements (cdr elements))
> 
>  	  (if (not subsequent-pass)
>  		  (setq subsequent-pass t)
> 		(setq rc (concat rc join-string))
> 		)
> 
> 	  (setq rc (concat rc active)))
> 	rc))
> 


  or using the cl package' `loop':

(loop for elts on (list "some" "file" "path")
      if (cdr elts)
      concat (concat (car elts) "/")
      else concat (car elts))


/Tom
-- 
"...and so, as the La-La of time plays with the Tinky-Winky of
destiny, and the Dipsy of fate sits on the Po of eternity."
						      -- Humph

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

* Re: Creating string from list of strings
  2002-10-14 14:00 ` Joe Casadonte
  2002-10-14 15:15   ` Tom Capey
@ 2002-10-14 15:35   ` Stefan Monnier <foo@acm.com>
  2002-10-14 17:09     ` Joe Casadonte
  2002-10-14 21:00     ` Bill Wohler
  1 sibling, 2 replies; 8+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2002-10-14 15:35 UTC (permalink / raw)


>>>>> "Joe" == Joe Casadonte <jcasadonte@northbound-train.com> writes:
> (defun my-perl-join (join-string elements)
    (mapconcat 'identity join-string element))


-- Stefan

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

* Re: Creating string from list of strings
  2002-10-14 15:35   ` Stefan Monnier <foo@acm.com>
@ 2002-10-14 17:09     ` Joe Casadonte
  2002-10-14 21:00     ` Bill Wohler
  1 sibling, 0 replies; 8+ messages in thread
From: Joe Casadonte @ 2002-10-14 17:09 UTC (permalink / raw)


On 14 Oct 2002, Stefan Monnier wrote:

>>>>>> "Joe" == Joe Casadonte <jcasadonte@northbound-train.com>
>>>>>> writes:
>> (defun my-perl-join (join-string elements)
>     (mapconcat 'identity join-string element))

Well, it should be:

   (mapconcat 'identity elements join-string)

as in:

   (mapconcat 'identity (list "foo" "bar" "baz") "/")

but it is tre cool nonetheless!  I never knew mapconcat could do
that.  Learn something new each day....

Thanks!

--
Regards,

joe
Joe Casadonte
jcasadonte@northbound-train.com

------------------------------------------------------------------------------
         Llama Fresh Farms => http://www.northbound-train.com
   Gay Media Resource List => http://www.northbound-train.com/gaymedia.html
            Perl for Win32 => http://www.northbound-train.com/perlwin32.html
               Emacs Stuff => http://www.northbound-train.com/emacs.html
          Music CD Trading => http://www.northbound-train.com/cdr.html
------------------------------------------------------------------------------
                       Live Free, that's the message!
------------------------------------------------------------------------------

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

* Re: Creating string from list of strings
  2002-10-14 15:35   ` Stefan Monnier <foo@acm.com>
  2002-10-14 17:09     ` Joe Casadonte
@ 2002-10-14 21:00     ` Bill Wohler
  1 sibling, 0 replies; 8+ messages in thread
From: Bill Wohler @ 2002-10-14 21:00 UTC (permalink / raw)


  Thanks to all of you for your answers.

  I went with 

    (mapconcat 'identity list "")

  I had seen mapconcat before, but couldn't quite make it work. The
  'identity parameter was the missing clue.

-- 
Bill Wohler <wohler@newt.com>  http://www.newt.com/wohler/  GnuPG ID:610BD9AD
Maintainer of comp.mail.mh FAQ and mh-e. Vote Libertarian!
If you're passed on the right, you're in the wrong lane.

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

end of thread, other threads:[~2002-10-14 21:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-10-14  6:26 Creating string from list of strings Bill Wohler
2002-10-14  7:00 ` Stefan Monnier <foo@acm.com>
2002-10-14  7:05 ` John Paul Wallington
2002-10-14 14:00 ` Joe Casadonte
2002-10-14 15:15   ` Tom Capey
2002-10-14 15:35   ` Stefan Monnier <foo@acm.com>
2002-10-14 17:09     ` Joe Casadonte
2002-10-14 21:00     ` Bill Wohler

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