* how would you do this in rc
@ 1991-12-20 0:09 Chris Siebenmann
1991-12-20 1:11 ` DaviD W. Sanderson
1991-12-20 12:21 ` David J. Fiander
0 siblings, 2 replies; 4+ messages in thread
From: Chris Siebenmann @ 1991-12-20 0:09 UTC (permalink / raw)
To: rc
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.]
- cks
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: how would you do this in rc
1991-12-20 0:09 how would you do this in rc Chris Siebenmann
@ 1991-12-20 1:11 ` DaviD W. Sanderson
1991-12-20 12:21 ` David J. Fiander
1 sibling, 0 replies; 4+ messages in thread
From: DaviD W. Sanderson @ 1991-12-20 1:11 UTC (permalink / raw)
To: rc
> 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?
This probably isn't as efficent as it could be, but it only uses one
external command.
fn index { # str list
str = $1 ans = '' {
shift
while(~ $str $*)
{
shift
ans=$ans^x # count in unary
}
expr $ans : '.*'
#-------
# Use this if you don't have expr
#
# echo `{echo $ans | tr -d '\012' | wc -c}
#-------
}
}
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) # 2
DaviD W. Sanderson (dws@cs.wisc.edu)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: how would you do this in rc
1991-12-20 0:09 how would you do this in rc Chris Siebenmann
1991-12-20 1:11 ` DaviD W. Sanderson
@ 1991-12-20 12:21 ` David J. Fiander
1 sibling, 0 replies; 4+ messages in thread
From: David J. Fiander @ 1991-12-20 12:21 UTC (permalink / raw)
To: Chris Siebenmann; +Cc: rc
>From: Chris Siebenmann <hawkwind.utcs.toronto.edu!cks@rutgers.uucp>
> 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: how would you do this in rc
@ 1991-12-20 1:27 Tom Culliton x2278
0 siblings, 0 replies; 4+ messages in thread
From: Tom Culliton x2278 @ 1991-12-20 1:27 UTC (permalink / raw)
To: cks; +Cc: rc
Use awk. ;-) ;-) ;-)
Anything else would involve shifting through a copy of the list and
keeping a count with bc or something, or using bc to compute an index.
This is just off the top of my head and hasn't been tested...
# Invoked as: l=$mylist s=$mystring list_index
# there is probably a MUCH better way
fn list_index {
# Is it even there?
if (! ~ $l $s) {
echo 0; return
}
# Go hunting for the first occurance
cnt=1
while (! ~ $l($cnt) $s) {
# This is a bit gross...
cnt=`{ echo $cnt + 1 | bc }
}
# Tell 'em where we found it
echo $cnt
}
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~1991-12-20 15:12 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-12-20 0:09 how would you do this in rc Chris Siebenmann
1991-12-20 1:11 ` DaviD W. Sanderson
1991-12-20 12:21 ` David J. Fiander
1991-12-20 1:27 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).