From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from news.lsuc.on.ca ([142.57.1.101]) by archone.tamu.edu with SMTP id <18886>; Fri, 20 Dec 1991 09:12:03 -0600 Received: by news.lsuc.on.ca (/\==/\ Smail3.1.24.1 #24.4) id ; Fri, 20 Dec 91 10:10 EST Received: by golem.uucp (/\==/\ Smail3.1.24.1 #24.13) id ; Fri, 20 Dec 1991 07:21 -0500 To: Chris Siebenmann cc: rc@archone.tamu.edu Subject: Re: how would you do this in rc In-reply-to: Your message of "Thu, 19 Dec 91 19:09:33 EST." <91Dec19.190941est.2742@hawkwind.utcs.toronto.edu> Date: Fri, 20 Dec 1991 06:21:09 -0600 Message-ID: <7071.693231669@golem.UUCP> From: "David J. Fiander" >From: Chris Siebenmann > You have a list l and a string s. What's the most efficient way to not >merely find out if s exists in l, but to find its index in l if it does? >Is there an efficient method to do this? If there isn't, should there be? :) >[probably not; rc is not a general programming language, after all.] I'd go with the obvious implementation, unless it is a bottleneck. i = 0 if (~ $s $l) { # if s is a member of l, then s matches an elt. i = 1 while (!~ $s $l($i)) { i = `{expr $i + 1} } } At the end of the loop (i == 0) if s is not a member, otherwise, i == the offset. If you wanted to avoid the expr overhead, and use byron's 'break' feature, then you could say for (i in `{seq $#l}) ... - David