Notifications
Clear all

[Closed] function to create array from array

hi all

i am really stuck into a problem.

i have an array which looks like this:
arr = (
3,

2,
2,
2,

3,
3,
2,
1,
4,
2,

2,
3,
1,
1,
1,
2,
2,
2,
1,
1,
2,
2,
2,
1,
2
)

i need a function that checks the first item of the array in this case 3. it takes this 3 to loop through the next 3 items and adds them together (in this case 2+2+2 = 6). than it loops 6 times through the next items and adds them together (in this case 3+3+2+1+4+2 = 15).

result array would be

#(#(3),#(2,2,2),#(3,3,2,1,4,2),#(2,3,1,1,1,2,2,2,1,1,2,2,2,1,2))
3 6 15

i am pretty sure that a recursive function would do the trick. but i just can`t figure out how this function should look like.

thanks for your help

boris

7 Replies
1 Reply
(@rustyknight)
Joined: 11 months ago

Posts: 0

The question begs, why…but that’s not why I’m here



-- Collects the values from the array, starting from startIndex and collects
-- count elements into a new array
-- Returns a new array of length count...
fn add arr startIndex count = (

	local result = #()
	
	-- I'm sure you could use collect, but never got my head around it...
	-- This should really check to see if we've past beyond the end of the
	-- source array...but I'm sure you can figure that out
	for iIndex = startIndex to (startIndex + count - 1) do (
	
		append result arr[iIndex]
	
	)
	
	return result
)

-- Sums the array and returns the result
fn sum arr = (

	format "sum - arr = %
" arr

	local iResult = 0

	for iValue in arr do (
	
		iResult += iValue
	
	)
	
	return iResult

)

-- This is the seeding function, it sets everything up and then gets everything running...
fn doStuff arr = (

	-- Create the basic array, with the first element from the source array
	local result = #(#(arr[1]))
	-- Set the start index
	local startIndex = 2
	-- Get the number of elements to be read...
	local count = arr[1]
	
	-- Loop until we exceed the end of the parent array
	while startIndex < arr.count do (
		-- Get the new collection of elements...
		local col = (add arr startIndex count)
		
		-- Move to the next point
		startIndex += count
		-- Figure out how many new elements we want
		count = (sum col)
	
		-- Append the new collection to our result
		append result col
	
	)

	return result
)

arr = #(
3,
------
2,
2,
2,
------
3,
3,
2,
1,
4,
2,
------
2,
3,
1,
1,
1,
2,
2,
2,
1,
1,
2,
2,
2,
1,
2
)

doStuff arr


I appologise, this is a hack…I’m sure there are any number of ways to clean it up, but not really knowing why you want it, this is jus the bare bones…

Shane

thank you very much. i am going to test it now.

boris

works perfectly. thank you again for your fast reply

i think this should work for you:

yourArray = #(3,2,2,2,3,3,2,1,4,2,2,3,1,1,1,2,2,2,1,1,2,2,2,1,2)


fn arrayFromArray theArray =
(
	newArray = #( #(theArray[1]) )
	
	startVal = newArray.count + 1
	endVal = newArray[1][1] + 1
	
	for n in newArray while theArray[startVal] != undefined do
	(
		tempArray = for i = startVal to endVal where theArray[i] != undefined collect theArray[i]
		append newArray tempArray
		
		startVal += tempArray.count
		for t in tempArray do endVal += t
	)
	
	newArray
)

test = arrayFromArray yourArray

let me know if there’s any problems

(
	sourceArray = #(3,2,2,2,3,3,2,1,4,2,2,3,1,1,1,2,2,2,1,1,2,2,2,1,2)
	newArray = #() --array to collect sub-arrays
	fn eatFromArray count =  --function to collect into sub-array
	(
		local newCount = 0 --counter for next call
		append newArray (for i = 1 to count collect ( --collect 'count' elements, append
			temp = sourceArray[1] --take the first element
			deleteItem sourceArray 1 --delete it from the source array
			newCount += temp --add its value to the counter
			temp )--end collect --and return it for collection
		)
		--if the source array has enough elements left, call recursively with new count
		if sourceArray.count >= newCount do eatFromArray newCount
	)--end fn 
	eatFromArray 1 --launch for first element
	newArray --return the result
)

Yet another form of the same, using recursive function call. (eatFromArray() calls itself)

i really appreciate the support of you all in this forum.
big thanks for sharing your knowledge and time.

boris

If you’re interested in studying recursion in an approachable yet in depth manner, I recommend The Little Schemer. It was designed for this educational task.