Re: type string

new topic     » topic index » view thread      » older message » newer message

Alan Tu <ATU5713 at COMPUSERVE.COM> wrote:

>Is there a better way to do this?
>
>type string(sequence s)
>    for i = 1 to length(s) do
>        if not (s[i] >= 0 and s[i] <= 255) then
>            return 0
>        end if
>    end for
>    return 1
>end type


The previous solutions for this problem overlooked a few possibilities.
Here's my solution:

 ------------------------------------------------------------------------
type string(sequence s)
    object c -- thanks Rod Jackson!

    for i = 1 to length(s) do
        c = s[i]
        if not integer(c) then
            -- strings cannot contain sequences or floating-point values
            return 0
        elsif c < 0 or c > 255 then
            -- not a byte value
            return 0
        end if
    end for

    -- if we've made it here, it's a string
    return 1
end type
 -------------------------------------------------------------------------

It took me quite a few false starts for me to get this completed. As you can
see, we have to look for *much* more than just the range of the values. We
have to check each element to be sure it's an integer value (and not a
floating-point value or even another sequence), *before* we can verify the
range of values. To my knowledge, there isn't any more "elegant" way of
doing this, though I'd be quite happy to be proven wrong.



Liquid-Nitrogen Software <nitrogen_069 at HOTMAIL.COM> wrote:

> --this should work:
>
>type string(sequence s)
>    if find(0, (s >= 0 and s <= 255)) then
>        return 0
>    else
>        return 1
>    end if
>end type


This is basically the first solution I arrived at, only I wrote it like
this...

 --------------------------------------------
type string(sequence s)
    return not find(0, s >= 0 and s <= 255))
end type
 --------------------------------------------

..which would be the *most* elegant way to do this, if we didn't have to
check for subsequences or floating-point values.



Gabriel Boehme

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu