unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* a function for string splitting
@ 2002-11-26 14:16 Luis O. Silva
  2002-11-26 20:03 ` Greg Hill
  0 siblings, 1 reply; 13+ messages in thread
From: Luis O. Silva @ 2002-11-26 14:16 UTC (permalink / raw)


Dear Emacs community,

I'm writing a function for translating dates in the form of a
string into Spanish and Russian. For example, you have:

"Thu, 21 Nov 2002 16:05:50 -0600 (CST)"

Within my function I used a `let' expression of the form:

(let ((day (substring "Thu, 21 Nov 2002 16:05:50 -0600 (CST)" 0 3))
       (month (substring "Thu, 21 Nov 2002 16:05:50 -0600 (CST)" 8 11)))
       ...)

All works fine provided that there isn't any date with
one-digit day, i. e., "Fri, 8 Nov 2002 11:56:37 -0500 (CST)"

My question is what function I could use for correctly
splitting the string.

Please be indulgent with me since

1. I'm not a programmer
2. I don't have access to the elisp manual even on-line (my
connection is very slow, only sufficient to download my
e-mail).

Thank you very much in advance
luis



-- 
Luis Octavio Silva P.
St. Petersburg State University.
66/3 Botanicheskaya St., Apt.119/2
Stary Peterhof
St. Petersburg, Russia.

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

* Re: a function for string splitting
       [not found] <mailman.1038319044.17125.help-gnu-emacs@gnu.org>
@ 2002-11-26 14:49 ` Lee Sau Dan
  2002-11-26 19:59   ` Luis O. Silva
  2002-11-26 15:56 ` Daniel Jensen
  1 sibling, 1 reply; 13+ messages in thread
From: Lee Sau Dan @ 2002-11-26 14:49 UTC (permalink / raw)


>>>>> "Luis" == Luis O Silva <silva@paloma.spbu.ru> writes:

    Luis> Dear Emacs community, I'm writing a function for translating
    Luis> dates in the form of a string into Spanish and Russian. For
    Luis> example, you have:

    Luis> "Thu, 21 Nov 2002 16:05:50 -0600 (CST)"

    Luis> Within my function I used a `let' expression of the form:

    Luis> (let ((day (substring "Thu, 21 Nov 2002 16:05:50 -0600
    Luis> (CST)" 0 3)) (month (substring "Thu, 21 Nov 2002 16:05:50
    Luis> -0600 (CST)" 8 11))) ...)

    Luis> All works fine provided that there isn't any date with
    Luis> one-digit day, i. e., "Fri, 8 Nov 2002 11:56:37 -0500 (CST)"

    Luis> My question is what function I could use for correctly
    Luis> splitting the string.

    Luis> Please be indulgent with me since

    Luis> 1. I'm not a programmer 2. I don't have access to the elisp
    Luis> manual even on-line (my connection is very slow, only
    Luis> sufficient to download my e-mail).

Consider learning "regular expressions",  which are not only useful in
Emacs,  but also useful  throughout the  unix environment  (e.g. grep,
sed, awk, vi, ...).

Try:

(let ((date-string "Thu, 21 Nov 2002 16:05:50 -0600")
      (regex-fragment-1 "\\([A-Z][a-z]*\\), ")
      (regex-fragment-2 "\\([0-9]+\\) \\([A-Z][a-z]*\\) \\([0-9]+\\) ")
      (regex-fragment-3 "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\) ")
      (regex-fragment-4 "\\([+-][0-9]*\\)" ))
  (if (string-match (concat regex-fragment-1
			    regex-fragment-2
			    regex-fragment-3
			    regex-fragment-4)
		    date-string)
    (let ((day-of-week (match-string 1 date-string))
	  (day (match-string 2 date-string))
	  (month-name (match-string 3 date-string))
	  (year (match-string 4 date-string))
	  (hour (match-string 5 date-string))
	  (minute (match-string 6 date-string))
	  (second (match-string 7 date-string))
	  (tz-spec (match-string 8 date-string)))

    (list day month-name year hour minute second tz-spec))))



Note that I've broken up the  regex into 4 fragments to make it usenet
friendly,  rejoining  them with  (concat  ...).   This  is not  really
necessary.


-- 
Lee Sau Dan                     李守敦(Big5)                    ~{@nJX6X~}(HZ) 

E-mail: danlee@informatik.uni-freiburg.de
Home page: http://www.informatik.uni-freiburg.de/~danlee

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

* Re: a function for string splitting
       [not found] <mailman.1038319044.17125.help-gnu-emacs@gnu.org>
  2002-11-26 14:49 ` Lee Sau Dan
@ 2002-11-26 15:56 ` Daniel Jensen
  2002-11-26 20:25   ` Luis O. Silva
       [not found]   ` <mailman.1038341287.29557.help-gnu-emacs@gnu.org>
  1 sibling, 2 replies; 13+ messages in thread
From: Daniel Jensen @ 2002-11-26 15:56 UTC (permalink / raw)


"Luis O. Silva" <silva@paloma.spbu.ru> writes:

> I'm writing a function for translating dates in the form of a
> string into Spanish and Russian. For example, you have:
>
> "Thu, 21 Nov 2002 16:05:50 -0600 (CST)"

You might want to use format-time-string instead. To get the above
date form but with localized month and day names, you would use:

(format-time-string "%a, %e %b %Y %H:%M:%S %z (%Z)")

> Within my function I used a `let' expression of the form:
>
> (let ((day (substring "Thu, 21 Nov 2002 16:05:50 -0600 (CST)" 0 3))
>        (month (substring "Thu, 21 Nov 2002 16:05:50 -0600 (CST)" 8 11)))
>        ...)
>
> All works fine provided that there isn't any date with
> one-digit day, i. e., "Fri, 8 Nov 2002 11:56:37 -0500 (CST)"
>
> My question is what function I could use for correctly
> splitting the string.

There is actually a split-string function.

-- 
Daniel Jensen
> (format (concat "mailto:" "%s@%s.%s") "daniel" "bigwalter" "net")

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

* Re: a function for string splitting
  2002-11-26 14:49 ` Lee Sau Dan
@ 2002-11-26 19:59   ` Luis O. Silva
  0 siblings, 0 replies; 13+ messages in thread
From: Luis O. Silva @ 2002-11-26 19:59 UTC (permalink / raw)
  Cc: help-gnu-emacs

Dear Lee,

On 26 Nov 2002 15:49:00 +0100, Lee Sau Dan writes:

   Luis> My question is what function I could use for
   Luis> correctly splitting the string.

   Luis> Please be indulgent with me since

   Luis> 1. I'm not a programmer 2. I don't have access to the
   Luis> elisp manual even on-line (my connection is very
   Luis> slow, only sufficient to download my e-mail).

   Lee> Consider learning "regular expressions", which are not
   Lee> only useful in Emacs, but also useful throughout the
   Lee> unix environment (e.g. grep, sed, awk, vi, ...).

   Lee> Try:

   Lee> (let ((date-string "Thu, 21 Nov 2002 16:05:50 -0600")
   Lee>       (regex-fragment-1 "\\([A-Z][a-z]*\\), ")
   Lee>       (regex-fragment-2 "\\([0-9]+\\)\\([A-Z][a-z]*\\)\\([0-9]+\\) ")
   Lee>       (regex-fragment-3 "\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\) ")
   Lee>       (regex-fragment-4 "\\([+-][0-9]*\\)" ))
   Lee>   (if (string-match (concat regex-fragment-1
   Lee>                             regex-fragment-2
   Lee>                             regex-fragment-3
   Lee>                             regex-fragment-4)
   Lee>                     date-string)
   Lee>     (let ((day-of-week (match-string 1 date-string))
   Lee>           (day (match-string 2 date-string))
   Lee>           (month-name (match-string 3 date-string))
   Lee>           (year (match-string 4 date-string))
   Lee>           (hour (match-string 5 date-string))
   Lee>           (minute (match-string 6 date-string))
   Lee>           (second (match-string 7 date-string))
   Lee>           (tz-spec (match-string 8 date-string)))

   Lee>     (list day month-name year hour minute second tz-spec))))

Thank you very much. This is just what I needed. I have an
incipient notion of regular expressions and I had used them to
do search in buffers. I didn't know of the existence of the
function `string-match'.

   Lee> Note that I've broken up the regex into 4 fragments to
   Lee> make it usenet friendly, rejoining them with (concat
   Lee> ...).  This is not really necessary.

Yes, it is not necessary but this nicety will also be useful
for me. Thanks a lot.

Regards,
luis

-- 
Luis Octavio Silva P.
St. Petersburg State University.
66/3 Botanicheskaya St., Apt.119/2
Stary Peterhof
St. Petersburg, Russia.

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

* Re: a function for string splitting
  2002-11-26 14:16 a function for string splitting Luis O. Silva
@ 2002-11-26 20:03 ` Greg Hill
  0 siblings, 0 replies; 13+ messages in thread
From: Greg Hill @ 2002-11-26 20:03 UTC (permalink / raw)


At 5:16 PM +0300 11/26/02, Luis O. Silva wrote:
>Dear Emacs community,
>
>I'm writing a function for translating dates in the form of a
>string into Spanish and Russian. For example, you have:
>
>"Thu, 21 Nov 2002 16:05:50 -0600 (CST)"
>
>Within my function I used a `let' expression of the form:
>
>(let ((day (substring "Thu, 21 Nov 2002 16:05:50 -0600 (CST)" 0 3))
>        (month (substring "Thu, 21 Nov 2002 16:05:50 -0600 (CST)" 8 11)))
>        ...)
>
>All works fine provided that there isn't any date with
>one-digit day, i. e., "Fri, 8 Nov 2002 11:56:37 -0500 (CST)"
>
>My question is what function I could use for correctly
>splitting the string.

Luis,

Give something like this a try:

(let*
  ((split-date (split-string "Thu, 21 Nov 2002 16:05:50 -0600 (CST)" " "))
  ((day-of-week (elt split-date 0))
  ((day-of-month (elt split-date 1))
  ((month (elt split-date 2))
  ...)

Be sure to use let* instead of let, since let does not guarentee the 
order of evaluation, and you always want split-date to be evaluated 
first.

--Greg

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

* Re: a function for string splitting
@ 2002-11-26 20:14 Greg Hill
  2002-11-27  5:40 ` Luis O. Silva
  0 siblings, 1 reply; 13+ messages in thread
From: Greg Hill @ 2002-11-26 20:14 UTC (permalink / raw)


Luis,
Oops, too many "(".  Make that:

(let*
  (split-date (split-string "Thu, 21 Nov 2002 16:05:50 -0600 (CST)" " "))
  (day-of-week (elt split-date 0))
  (day-of-month (elt split-date 1))
  (month (elt split-date 2))
  ...)

--Greg

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

* Re: a function for string splitting
  2002-11-26 15:56 ` Daniel Jensen
@ 2002-11-26 20:25   ` Luis O. Silva
       [not found]   ` <mailman.1038341287.29557.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 13+ messages in thread
From: Luis O. Silva @ 2002-11-26 20:25 UTC (permalink / raw)
  Cc: help-gnu-emacs

Dear Daniel,

On Tue, 26 Nov 2002 16:56:36 +0100, Daniel Jensen writes:

   Daniel> "Luis O. Silva" <silva@paloma.spbu.ru> writes:
   >> I'm writing a function for translating dates in the form
   >> of a string into Spanish and Russian. For example, you
   >> have:
   >> "Thu, 21 Nov 2002 16:05:50 -0600 (CST)"

   Daniel> You might want to use format-time-string
   Daniel> instead. To get the above date form but with
   Daniel> localized month and day names, you would use:

   Daniel> (format-time-string "%a, %e %b %Y %H:%M:%S %z
   Daniel> (%Z)")

Thank you for your suggestion, the problem here is that my
starting point is a string. Lee suggested what I needed.

Thank you.
luis

-- 
Luis Octavio Silva P.
St. Petersburg State University.
66/3 Botanicheskaya St., Apt.119/2
Stary Peterhof
St. Petersburg, Russia.

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

* Re: a function for string splitting
  2002-11-26 20:14 Greg Hill
@ 2002-11-27  5:40 ` Luis O. Silva
  0 siblings, 0 replies; 13+ messages in thread
From: Luis O. Silva @ 2002-11-27  5:40 UTC (permalink / raw)
  Cc: help-gnu-emacs

Dear Greg,

On Tue, 26 Nov 2002 12:14:49 -0800, Greg Hill writes:

 Greg> Luis, Oops, too many "(".  Make that:

 Greg> (let*
 Greg>  (split-date (split-string "Thu, 21 Nov 2002 16:05:50 -0600 (CST)" " "))
 Greg>  (day-of-week (elt split-date 0))
 Greg>  (day-of-month (elt split-date 1))
 Greg>  (month (elt split-date 2)) ...)

This is a very very interesting alternative suggestion to the
`string-match', `match-string' pair suggested before. Neat!

Thank you very much.

luis



-- 
Luis Octavio Silva P.
St. Petersburg State University.
66/3 Botanicheskaya St., Apt.119/2
Stary Peterhof
St. Petersburg, Russia.

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

* Re: a function for string splitting
       [not found]   ` <mailman.1038341287.29557.help-gnu-emacs@gnu.org>
@ 2002-11-28 14:14     ` Kai Großjohann
  2002-11-28 18:21       ` Luis O. Silva
       [not found]       ` <mailman.1038506557.25888.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 13+ messages in thread
From: Kai Großjohann @ 2002-11-28 14:14 UTC (permalink / raw)


"Luis O. Silva" <silva@paloma.spbu.ru> writes:

> Thank you for your suggestion, the problem here is that my
> starting point is a string. Lee suggested what I needed.

parse-time-string appears to be useful for in that case.  Using
parse-time-string, you convert the date into an internal
representation, then using format-time-string, you convert it back
into the external representation.

-- 
~/.signature is: umop ap!sdn    (Frank Nobis)

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

* Re: a function for string splitting
  2002-11-28 14:14     ` Kai Großjohann
@ 2002-11-28 18:21       ` Luis O. Silva
       [not found]       ` <mailman.1038506557.25888.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 13+ messages in thread
From: Luis O. Silva @ 2002-11-28 18:21 UTC (permalink / raw)


Hello Kai,

On Thu, 28 Nov 2002 15:14:48 +0100, Kai Großjohann writes:

   Kai> "Luis O. Silva" <silva@paloma.spbu.ru> writes:
   >> Thank you for your suggestion, the problem here is that
   >> my starting point is a string. Lee suggested what I
   >> needed.

   Kai> parse-time-string appears to be useful for in that
   Kai> case.  Using parse-time-string, you convert the date
   Kai> into an internal representation, then using
   Kai> format-time-string, you convert it back into the
   Kai> external representation.

I didn't find the function `parse-time-string' in my Emacs
system (21.1.1). It seems that I need an upgrade.

Thanks,
luis



-- 
Luis Octavio Silva P.
St. Petersburg State University.
66/3 Botanicheskaya St., Apt.119/2
Stary Peterhof
St. Petersburg, Russia.

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

* Re: a function for string splitting
       [not found]       ` <mailman.1038506557.25888.help-gnu-emacs@gnu.org>
@ 2002-11-28 18:30         ` Benjamin Lewis
  2002-11-28 18:50         ` Kai Großjohann
  1 sibling, 0 replies; 13+ messages in thread
From: Benjamin Lewis @ 2002-11-28 18:30 UTC (permalink / raw)


On Thu, 28 Nov 2002, Luis O. Silva wrote:

> I didn't find the function `parse-time-string' in my Emacs system
> (21.1.1). It seems that I need an upgrade.

How did you look for it?  Note that it not an interactive function.  Did
you try doing a C-h f parse-time-string <RET> ?

-- 
Benjamin Lewis

Clothes make the man.  Naked people have little or no influence on society.
- Mark Twain

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

* Re: a function for string splitting
       [not found]       ` <mailman.1038506557.25888.help-gnu-emacs@gnu.org>
  2002-11-28 18:30         ` Benjamin Lewis
@ 2002-11-28 18:50         ` Kai Großjohann
  2002-11-29  7:03           ` Luis O. Silva
  1 sibling, 1 reply; 13+ messages in thread
From: Kai Großjohann @ 2002-11-28 18:50 UTC (permalink / raw)


"Luis O. Silva" <silva@paloma.spbu.ru> writes:

> I didn't find the function `parse-time-string' in my Emacs
> system (21.1.1). It seems that I need an upgrade.

Maybe it is sufficient to say M-: (require 'parse-time) RET.
Then parse-time-string should be defined.

-- 
~/.signature is: umop ap!sdn    (Frank Nobis)

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

* Re: a function for string splitting
  2002-11-28 18:50         ` Kai Großjohann
@ 2002-11-29  7:03           ` Luis O. Silva
  0 siblings, 0 replies; 13+ messages in thread
From: Luis O. Silva @ 2002-11-29  7:03 UTC (permalink / raw)


On Thu, 28 Nov 2002 19:50:05 +0100, Kai Großjohann writes:

   Kai> Maybe it is sufficient to say M-: (require
   Kai> 'parse-time) RET.  Then parse-time-string should be
   Kai> defined.

Yes, Thank you. I had searched with `C-h f' without
success. After loading parse-time I found the
function. `parse-time-string' works greatly.

Thank you again,
luis


-- 
Luis Octavio Silva P.
St. Petersburg State University.
66/3 Botanicheskaya St., Apt.119/2
Stary Peterhof
St. Petersburg, Russia.

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

end of thread, other threads:[~2002-11-29  7:03 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-11-26 14:16 a function for string splitting Luis O. Silva
2002-11-26 20:03 ` Greg Hill
     [not found] <mailman.1038319044.17125.help-gnu-emacs@gnu.org>
2002-11-26 14:49 ` Lee Sau Dan
2002-11-26 19:59   ` Luis O. Silva
2002-11-26 15:56 ` Daniel Jensen
2002-11-26 20:25   ` Luis O. Silva
     [not found]   ` <mailman.1038341287.29557.help-gnu-emacs@gnu.org>
2002-11-28 14:14     ` Kai Großjohann
2002-11-28 18:21       ` Luis O. Silva
     [not found]       ` <mailman.1038506557.25888.help-gnu-emacs@gnu.org>
2002-11-28 18:30         ` Benjamin Lewis
2002-11-28 18:50         ` Kai Großjohann
2002-11-29  7:03           ` Luis O. Silva
  -- strict thread matches above, loose matches on Subject: below --
2002-11-26 20:14 Greg Hill
2002-11-27  5:40 ` Luis O. Silva

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