rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
* new fn index
@ 1991-12-20  1:40 DaviD W. Sanderson
  0 siblings, 0 replies; 2+ messages in thread
From: DaviD W. Sanderson @ 1991-12-20  1:40 UTC (permalink / raw)
  To: rc

Well, I stuck out my neck and Chris showed me how to eliminate
even the one use of an external command.  That'll teach me! :-)

#-------
# index - print the index of the given string in the given list.
# (0 if not there)
# If the string occurs more than once, the index of the last occurrence
# is printed.
#-------
fn index {	# str list
	str = $1 ans = () {
		shift
		while(~ $str $*)
		{
			#-------
			# count in unary
			# May as well keep track of what I'm shifting
			# (perhaps it will be useful in a different
			# version...).
			#-------
			ans=($ans $1)
			shift
		}
		echo $#ans
	}
}

index a (x y z)		# 0
index a (a y z)		# 1
index a (x a z)		# 2
index a (ax y a)	# 3
index '' (a '' b '')	# 4


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

* Re: new fn index
@ 1991-12-20  2:17 Tom Culliton x2278
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Culliton x2278 @ 1991-12-20  2:17 UTC (permalink / raw)
  To: dws; +Cc: rc

Make that while test

	while (! ~ $str $1)

and you can find the first occurance


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

end of thread, other threads:[~1991-12-20  2:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-12-20  1:40 new fn index DaviD W. Sanderson
1991-12-20  2:17 Tom Culliton x2278

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 NNTP newsgroup(s).