World: r4wp
[Rebol School] REBOL School
older newer | first last |
Sujoy 3-Jul-2012 [564] | shouldnt sort/skip/compare/all comps 2 :sf pick up the object? |
Henrik 3-Jul-2012 [565x2] | I'm not sure if it's possible, but you may then need to use 4 parameters instead of 2 in the compare function. Then use the string/object pairs for comparison. |
I'm not certain that SKIP can be used with COMPARE. | |
Sujoy 3-Jul-2012 [567] | ah - so i should do: >> sf: func [a b c d] [ o: select b/attribs "1991-1992" m: select d/attribs "1991-1992" o/m > m/m] ?? |
Henrik 3-Jul-2012 [568] | Untested, but yes, that's what I meant. I really don't know if it works. |
Sujoy 3-Jul-2012 [569x2] | i get: >> sort/compare comps :sf ** Script Error: Invalid argument: ?function? ** Near: sort/compare comps :sf |
>> sort/compare/all comps :sf ** Script Error: Invalid argument: ?function? ** Near: sort/compare comps :sf | |
Arnold 3-Jul-2012 [571] | Back to the drawing board Sujoy and displaying the elements you want to use in your function piece by piece and maybe dump-obj is of any use? (Now you don't use a and c) |
Sujoy 3-Jul-2012 [572] | the sort/compare works great if its an object...am stuck because its a hash |
Henrik 3-Jul-2012 [573] | Testing your code. What is COMPS? |
Sujoy 3-Jul-2012 [574] | sorry - comps should be h - the hash |
Henrik 3-Jul-2012 [575] | ok, the sort function must only have 2 parameters. |
Sujoy 3-Jul-2012 [576] | not quite understanding how to use sort with this structure... |
Henrik 3-Jul-2012 [577] | The structure is a little strange. Are you trying to sort the outer hash or the inner hash? |
Sujoy 3-Jul-2012 [578x2] | both actually - need the outer hash sorted based on a property of the object inside the inner hash |
ideally, would like to keep the hash h intact for retrieval by the keys | |
Henrik 3-Jul-2012 [580] | The data you have pasted, contains two identical objects, so am not sure what the outcome is supposed to be. |
Sujoy 3-Jul-2012 [581x3] | here's the hash closer to reality: #[hash!["20Th Century Finance Corpn. Ltd. [Merged]" #[object! [ name: "20Th Century Finance Corpn. Ltd. [Merged]" mcapz: #[hash!["1993-1994" #[object! [ yyyymmdd: 30-Sep-1993 rebal-year: 1993 ln-mcap: 5.07528783998276 mcap: 160.018245 ltmto: 0 ]] "1992-1993" #[object! [ yyyymmdd: 30-Sep-1992 rebal-year: 1992 ln-mcap: 4.64720424566052 mcap: 104.293 ltmto: 0 ]] "1991-1992" #[object! [ yyyymmdd: 27-Sep-1991 rebal-year: 1991 ln-mcap: 3.28196329373846 mcap: 26.628 ltmto: 0 ]] "1996-1997" #[object! [ yyyymmdd: 30-Sep-1996 rebal-year: 1996 ln-mcap: 4.22786241308827 mcap: 68.5705 ltmto: 4.4951 ]] "1995-1996" #[object! [ yyyymmdd: 29-Sep-1995 rebal-year: 1995 ln-mcap: 4.6522555263423 mcap: 104.821146 ltmto: 1.9152 ]] "1994-1995" #[object! [ yyyymmdd: 30-Sep-1994 rebal-year: 1994 ln-mcap: 5.60133608288591 mcap: 270.787961 ltmto: 0 ]] "1999-2000" #[object! [ yyyymmdd: 1-Sep-1999 rebal-year: 1999 ln-mcap: #[none] mcap: #[none] ltmto: 0.3847 ]] "1998-1999" #[object! [ yyyymmdd: 30-Sep-1998 rebal-year: 1998 ln-mcap: 3.26596118430253 mcap: 26.205287 ltmto: 1.8029 ]] "1997-1998" #[object! [ yyyymmdd: 30-Sep-1997 rebal-year: 1997 ln-mcap: 3.70636432395486 mcap: 40.705545 ltmto: 4.0871 ]]]] ]] "3M India Ltd." #[object! [ name: "3M India Ltd." mcapz: #[hash!["1993-1994" #[object! [ yyyymmdd: 30-Sep-1993 rebal-year: 1993 ln-mcap: 5.2695189287062 mcap: 194.322457 ltmto: 0 ]] "1992-1993" #[object! [ yyyymmdd: 30-Sep-1992 rebal-year: 1992 ln-mcap: 5.16365007188814 mcap: 174.80133 ltmto: 0 ]] "1991-1992" #[object! [ yyyymmdd: 27-Sep-1991 rebal-year: 1991 ln-mcap: 5.13698182480598 mcap: 170.201295 ltmto: 0 ]] "1994-1995" #[object! [ yyyymmdd: 30-Sep-1994 rebal-year: 1994 ln-mcap: 5.67980332582336 mcap: 292.89182 ltmto: 0 ]] "1995-1996" #[object! [ yyyymmdd: 29-Sep-1995 rebal-year: 1995 ln-mcap: 5.36086870986747 mcap: 212.909823 ltmto: 2.438 ]] "1996-1997" #[object! [ yyyymmdd: 27-Sep-1996 rebal-year: 1996 ln-mcap: 5.29527142738166 mcap: 199.391739 ltmto: 13.7717 ]] "1999-2000" #[object! [ yyyymmdd: 30-Sep-1999 rebal-year: 1999 ln-mcap: 6.62266108574718 mcap: 751.943423 ltmto: 70.8157 ]] "1998-1999" #[object! [ yyyymmdd: 30-Sep-1998 rebal-year: 1998 ln-mcap: 5.59892693888198 mcap: 270.136379 ltmto: 12.1941 ]] "1997-1998" #[object! [ yyyymmdd: 30-Sep-1997 rebal-year: 1997 ln-mcap: 5.41116944722567 mcap: 223.893266 ltmto: 10.31 ]] "2002-2003" #[object! [ yyyymmdd: 30-Sep-2002 rebal-year: 2002 ln-mcap: 5.87169433363346 mcap: 354.849705 ltmto: 18.495901 ]] "2001-2002" #[object! [ yyyymmdd: 28-Sep-2001 rebal-year: 2001 ln-mcap: 5.70261800358953 mcap: 299.650862 ltmto: 17.817 ]] "2000-2001" #[object! [ yyyymmdd: 29-Sep-2000 rebal-year: 2000 ln-mcap: 6.51937914011665 mcap: 678.157214 ltmto: 39.9108 ]] "2003-2004" #[object! [ yyyymmdd: 30-Sep-2003 rebal-year: 2003 ln-mcap: 6.14402101757279 mcap: 465.923295 ltmto: 22.518454 ]] "2004-2005" #[object! [ yyyymmdd: 30-Sep-2004 rebal-year: 2004 ln-mcap: 6.36648735824138 mcap: 582.009842 ltmto: 18.47743 ]] "2005-2006" #[object! [ yyyymmdd: 30-Sep-2005 rebal-year: 2005 ln-mcap: 6.89448778574422 mcap: 986.820132 ltmto: 76.96006 ]] "2007-2008" #[object! [ yyyymmdd: 28-Sep-2007 rebal-year: 2007 ln-mcap: 7.61782530207882 mcap: 2034.13369 ltmto: 565.232151 ]] "2006-2007" #[object! [ yyyymmdd: 29-Sep-2006 rebal-year: 2006 ln-mcap: 7.29216005157061 mcap: 1468.739827 ltmto: 326.743221999999 ]] "2008-2009" #[object! [ yyyymmdd: 30-Sep-2008 rebal-year: 2008 ln-mcap: 7.3914506403371 mcap: 1622.057429 ltmto: 244.97382 ]] "2010-2011" #[object! [ yyyymmdd: 30-Sep-2010 rebal-year: 2010 ln-mcap: 8.40720356304076 mcap: 4479.217133 ltmto: 123.100087 ]] "2009-2010" #[object! [ yyyymmdd: 30-Sep-2009 rebal-year: 2009 ln-mcap: 7.58070461394744 mcap: 1960.009529 ltmto: 91.0568390000001 ]] "2011-2012" #[object! [ yyyymmdd: 30-Sep-2011 rebal-year: 2011 ln-mcap: 8.45073201719186 mcap: 4678.496222 ltmto: 282.168385 ]]]] ]] |
i need to sort the hash based on a param (mcap) for an input year (say "1991-1992") | |
then do things like 1. select the top 50% 2. calculate interquartile ranges etc. | |
Henrik 3-Jul-2012 [584] | Why do the brackets not match... |
Sujoy 3-Jul-2012 [585] | apologies again - extracted this fragment from a gzillion mb file - there should be ]]]] at the end |
Henrik 3-Jul-2012 [586] | aha, skipping does work, but the comparison happens on the strings instead of the objects. |
Sujoy 3-Jul-2012 [587x2] | yes... >> sort/skip/compare/all comps 2 func [a b][a > b ] ..sorts in reverse order |
i cant seem to reference the object! | |
Henrik 3-Jul-2012 [589] | is MCAPZ the same as ATTRIBS? |
Sujoy 3-Jul-2012 [590x3] | yes |
if i do: >> sort/skip/compare/all comps 2 func [a b][length? a/mcapz > length? b/mcapz ] i get an invalid path error | |
and what i need to do is more complex than that :( | |
Henrik 3-Jul-2012 [593] | you can do a REVERSE H before and after the sorting. That allows SORT to grab the objects. |
Sujoy 3-Jul-2012 [594x2] | let me try... |
>> reverse comps sort/skip/compare/all comps 2 func [a b][length? a/mcapz > length? b/mcapz ] ** Script Error: Invalid path value: mcapz ** Near: length? a/mcapz > length? b/mcapz | |
Henrik 3-Jul-2012 [596] | Try: func [a b][probe type? a probe type? b length? a/mcapz > length? b/mcapz ] |
Maxim 3-Jul-2012 [597] | when you use /all, you get two BLOCKS. one for each record of length /skip so your function is called like so: sort-func [ "2009-2010" #[object! [ ... ]] ] [ "2011-2012" #[object! [ ... ]] ] |
Henrik 3-Jul-2012 [598] | aha, that means you don't need the REVERSE. You just need to reference the values differently. |
Sujoy 3-Jul-2012 [599] | reverse comps sort/skip/compare/all comps 2 func [a b][probe type? a probe type? b length? a/mcapz > length? b/mcapz ] block! block! ** Script Error: Invalid path value: mcapz ** Near: length? a/mcapz > length? b/mcapz |
Henrik 3-Jul-2012 [600] | Maxim's SORT-FUNC block shows you how to reference the values properly. |
Maxim 3-Jul-2012 [601x2] | my last example above with ?? added shows this clearly: >> sort/skip/compare/all [1 [2 "a"] 0 [4 "z"] 5 [4 "m"]] 2 func [a b][ ?? a ?? b a/2/2 < b/2/2 ] a: [0 [4 "z"]] b: [5 [4 "m"]] a: [1 [2 "a"]] b: [0 [4 "z"]] a: [5 [4 "m"]] b: [1 [2 "a"]] == [1 [2 "a"] 5 [4 "m"] 0 [4 "z"]] what is part of the record is completely irrelevant to sort, it transfers all control to your function. |
the only really problem with a compare func is that you must be sure that your data can be compared (which is not true for all types. ex: true/false is not defined for all comparison ops: >> true > false ** Script Error: Cannot use greater? on logic! value ** Near: true > false | |
Sujoy 3-Jul-2012 [603x2] | am getting somewhere (i think) >> sort/skip/compare/all comps 2 func [a b][o: a/2/mcapz probe o] === make hash! ["2003-2004" make object! [ yyyymmdd: 30-Sep-2003 rebal-year: 2003 ...]] etc |
so it is a referencing problem | |
Maxim 3-Jul-2012 [605] | yep... its the /2/ which is important here <:-) |
Sujoy 3-Jul-2012 [606x5] | just discovered another problem though...not all inner hashes have the same years - so one objects inner hash may have key "1991-1992" while another may not |
looks like i will have to collect the objects/inner-hash-objects into a separate collection, then apply the sorting on the new collection | |
c: copy[] foreach [k v] comps [if select v/mcapz "1991-1992" [append c v]] or some such | |
then use maxim's sort-func on c instead | |
is that generally the right direction? i would have ideally liked to have avoided creating new blocks to conserve memory... | |
Maxim 3-Jul-2012 [611] | you can simply say that when an entry doesn't have a year, it is always larger, so you have them at the end of the list. you could then sort those without a date according to their name. if you want just the list for "1991-1992" yes, that is a good approach... however usually, the fastest way to filter-out lists, is to copy the block (not deep, so its quick) and then use remove-each on the new block... like-so: filtered-data: copy data remove-each [hash mcapz] filtered-data [ not select mcapz "1991-1992"] |
Sujoy 3-Jul-2012 [612x2] | will try with both approaches. thanks for the filtered-data tip maxim - will definitely move to that! |
anyone know which one of these would be the correct one to use? 1. percentile: func[s p /local n][n: round/floor (p * (length? s) + 0.5) return s/(n - 1)] 2. percentile: func[s p /local n][n: round/floor (p * (length? s) + 0.5) return s/(n)] | |
older newer | first last |