RE: Is subscripting a slice useful

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

> Sure it might be convenient, but it becomes more trouble to decipher it, 
> than it is to just write it out the hard way in the first place.

Speaking of, didn't Lomax.Pete already provide a sufficiently efficient
function for this? Anyway, here's some code that does it simply:

--start vertical slicing code here
function randomWord()
sequence words
words =
{"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","absolute","basketball","crazy",
"delirious","effective","fixated","governing","hill","indigo","justified","Klingon","lemon","manners",
"neophyte","opposed","perfect","quagmire","rooster","safety","terror","umbrella","vibrate","wanton",
			 "xylophone","yellow","zebra"}
	return words[rand(length(words))]
end function

function dbSequence()
integer x, recs
sequence allRecs
	x = rand(10) + 10 -- between 11 and 20 fields per record
	recs = rand(90) + 10 -- between 11 and 100 records
	allRecs = repeat({},recs)
	for t=1 to recs do
		for u=1 to x do
			allRecs[t] = append(allRecs[t],randomWord())
		end for
	end for
	return allRecs
end function

function getColumn(atom which, sequence what)
sequence result
    result = {}
    if which > 0 and integer(which) then
        for t=1 to length(what) do
            if which <= length(what[t]) then
                result = append(result, what[t][which])
            else
                -- an error, but what to do about it?
                -- for now, return empty sequence
                result = append(result,"")
            end if
        end for
    end if
    return result
end function

sequence x, mySequence
mySequence = dbSequence() -- a db sequence is one with consistent lengths
                          -- for all elements of the db
x = getColumn(1, mySequence) -- getColumn #1 of all "records" in mySequence

puts(1,"All items in column 1:\n")
?x
?1/0
--end vertical slicing code here

I guess it would be better to call the function

   verts() -- for VERTical Slice

eh? Plus, it's shorter. :)

Then it becomes

    x = verts(1,mySequence)

vs.

    x = mySequence[1..$][1] -- or however you would do it... :/

both of which are the same length, so no time difference to the developer,
which is the most important consideration of all! :)

-=ck
"Programming in a state of EUPHORIA."
http://www.cklester.com/euphoria/

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

Search



Quick Links

User menu

Not signed in.

Misc Menu