Notifications
Clear all

[Closed] find biggest values in the array

Hi,

I’m totally exhausted by this and I’m done for today. I think my brain has melted for the last couple of days. So…

What I intend to do is to have a function “view top versions only” in my pipeline tool. Which just make to keep only files in top versions.

This is how far I’ve gone:

arr = #(
"Batman8_assets_FX_DesertDustFX_CoolFX_V01_001.max",
"Batman8_assets_FX_DesertDustFX_dustAsset_V01_001.max",
"Batman8_assets_FX_DesertDustFX_dustAsset_V01_002.max",
"Batman8_assets_FX_DesertDustFX_dustAsset_V01_003.max",
"Batman8_assets_FX_DesertDustFX_dustAsset_V01_004.max",
"Batman8_assets_FX_DesertDustFX_dustAsset_V01_005.max",
"Batman8_assets_FX_DesertDustFX_dustAsset_V02_001.max",
"Batman8_assets_FX_DesertDustFX_dustAsset_V02_002.max",
"Batman8_assets_FX_DesertDustFX_dustAsset_V02_003.max"
)

fn set3digit val = (
	tmp = "00" + (val as string)
	tmp = substring tmp (tmp.count-2) 3
	tmp
)

fn getTopVer arr = (
-- 		tmpArr = sort arr
	tmpArr = #()
	tmpArr2 = #()
	tmpArr3 = #()
	tmpArr4 = #()
	tmpArr5 = #()
	
	for obj in arr do (
		tmp = substring obj 1 (obj.count - 4)
		append tmpArr tmp
	)
	for obj in tmpArr do (
		a1 = substring obj 1 (obj.count - 4)
		a2 = substring obj (obj.count - 2) 3
		append tmpArr2 a1
		append tmpArr3 a2
-- 			print a1
-- 			print a2
	)
	for j=1 to tmpArr2.count do (
		tmpArr4 = #()
		for i=1 to tmpArr2.count do (
			if tmpArr2[j]==tmpArr2[i] do (
-- 					format "% == %
" tmpArr2[j] tmpArr2[i]
				append tmpArr4 tmpArr3[j]
			)
		)
		tmp2 = set3digit tmpArr4.count
		tmp = tmpArr2[j] + tmp2
		append tmpArr5 tmp
	)
	tmpArr5
)

print (getTopVer arr)

As you can see it’s pretty close. But…

  1. How can I make a function to get rid of “double” items? I can compare if names are the same and deleteItem, but this way it deletes when there are single versions.
  2. It completely fails when I’m missing a file, eg. v001, v002, v018
  3. Do you know any easier way than my getTopVer() function? It seems to be too complicated. Ofcourse simple sort wont work, because I have different files in the array
1 Reply

it’s pretty easy if you always stick with the same naming convention, where:

the first part is and by “V”

and the second part contains only formatted numbers and “_”

so let’s split the name (where the name is file part of the path – getfilefrompath)

well … the fist part

base = trimright name "0123456789_"

the second part:

version = substring name (base.count+1) -1

now the second part already sortable as a string but let’s convert it to number:

vernum = (substitutestring version "_" "") as integer

now we need a simple structure like:


struct FileStruct 
(
    file, name, base, version,
    on create do if this.file != undefined do 
    (
         name = tolower (getfilefrompath file)
         base = trimright name "0123456789_"
         version = (substitutestring (substring name (base.count+1) -1) "_" "") as integer
    )
)

and collect you files as structs:

_files = for file in files collect (FileStruct file:file)

and only thing left to do is to sort them…

we need two sort functions – by base and by version, where version sort is in descendant order.
well…

fn sortByVersion d1 d2 = (d2.version - d1.version) 
fn sortByBase d1 d2 = if (v = stricmp d1.base d2.base) == 0 then sortByVersion d1 d2 else v

and sort structured files:

qsort _files sortByBase

after that everything is simple…

(i’ve wrote it without test in max because i don’t have max around. so get it as a ‘pseudo’ code)