Euphoria Ticket #747: valid_index(), sequence of indexes

It would be useful if the valid_index() function could accept a sequence of indexes as a second argument (eg. to check index existence before using it as an argument of fetch() or store() functions)

Desirable behavior:

sequence s = {1,2,{3,4,5,{6,7}},8,9} 
? valid_index(s,{1,2}) -- output: 0 
? valid_index(s,{3,2}) -- output: 1 
? valid_index{s,{3,4,1}) -- output: 1 

Details

Type: Feature Request Severity: Normal Category: Library Routine
Assigned To: unknown Status: New Reported Release:
Fixed in SVN #: View VCS: none Milestone:

1. Comment by Insolor Feb 03, 2012

As a first approach:

public function valid_index(sequence st, object x) 
    if atom(x) then 
        if x < 1 then 
            return 0 
        end if 
        if floor(x) > length(st) then 
            return 0 
        end if 
        return 1 
    else 
        for i=1 to length(x) do 
            if atom(st) or sequence(x[i]) or not valid_index(st,x[i]) then 
                return 0 
            end if 
            st = st[x[i]] 
        end for 
        return 1 
    end if 
end function 

2. Comment by Insolor Feb 04, 2012

The second approach:

public function valid_index(object st, object x)
    if atom(x) then 
        stdseq:valid_index(st,x) 
    else 
        if atom(st) then 
            return 0 
        end if 
        for i=1 to length(x) do 
            if atom(st) or sequence(x[i]) or not stdseq:valid_index(st,x[i]) then 
                return 0 
            end if 
            st = st[x[i]] 
        end for 
    end if 
    return 1 
end function

Search



Quick Links

User menu

Not signed in.

Misc Menu