[Omake] splitting a string
Jason Hickey
jyh at cs.caltech.edu
Wed Sep 13 11:17:38 PDT 2006
You can use bare-bones programming to do what you want (in general I
think we want a more general string manipulation library). Here is a
code fragment.
<script>
#!/usr/bin/env osh
explode-string(s) =
# Make sure it is really a single string
s = $"$s"
# Collect the chars
len = $(s.length)
i = 0
explode[] =
while $(lt $i, $(len))
explode[] += '$(s.nth $i)'
i = $(add $i, 1)
export
value $(explode)
println($(concat \,, $(explode-string foo bar)))
</script>
I'm not sure if your release has while-loops. If it doesn't, then the
inner loop would need to be programmed as a recursive function, like this.
explode-loop(s, explode, i, len) =
if $(lt $i, $(len))
explode[] += $(s.nth $i)
explode-loop(s, $(explode), $(add $i, 1), $(len))
else
value $(explode)
Jason
David Kågedal wrote:
> I'm trying to convert some shell scripts in my old Make rules to
> omake. One thing I need to do is to split a string into a sequence of
> characters, or a sequence of 1-char strings if that makes more sense.
>
> The old code does
>
> x="`echo $$str | sed "s/./\'&\',/g"`"
>
> which will convert
>
> foo bar
>
> into
>
> 'f','o','o',' ','b','a','r',
>
> I tried the obvious S = $(S.map $(fun x, $x\,)), but that doesn't
> iterate over the characters. The split function can't be used
> either. So how would I go about to get at the individual characters
> of a string?
>
--
Jason Hickey http://www.cs.caltech.edu/~jyh
Caltech Computer Science Tel: 626-395-6568 FAX: 626-792-4257
More information about the Omake
mailing list