[comp.text.tex] Want to extract words from a string

jeff@zem.uchicago.edu (Jeff Adler) (01/31/91)

Suppose I have a string consisting of words separated by blanks.
I would like a macro which returns the last word of the string,
and another macro which returns everything else.

Example:
	\lastword{one two three four}  yields		four
	\butlastword{one two three four} yields		one two three

Also, if it isn't too complicated, it would be nice if these
macros ignored blanks inside nested braces.

Example:
	\lastword{one two three four\footnote{five six}}
		yields		four\footnote{five six}

I'd appreciate any help anyone out there can give.
Please respond by e-mail, and I'll summarize.
---------------------------------------------------------------------------
Jeff Adler				jeff@zaphod.uchicago.edu
Math Department				jeff%zaphod@midway.uchicago.edu
University of Chicago			jda3@midway.uchicago.edu

eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) (01/31/91)

jeff@zem.uchicago.edu (Jeff Adler) writes:

>Suppose I have a string consisting of words separated by blanks.
>I would like a macro which returns the last word of the string,

>Example:
>	\lastword{one two three four}  yields		four

\def\lastword#1{\def\compare{zzz}%
    \xlastword#1 zzz }
%               ^   ^ two spaces

\def\xlastword#1 #2 {%
%               ^  ^ corresponding to these
    \def\test{#2}%
    \ifx\test\compare #1%
    \else \expandafter\xlastword \fi #2 }
%                                      ^ significant space
    
>and another macro which returns everything else.

replace \ifx line by

    \ifx\test\compare \accu
    \else \edef\accu{\accu #1 }%
          \expandafter\xlastwork \fi #2 }

and add \def\accu{ } somewhere in the beginning.

Victor.