Re: 0-based Indexing
- Posted by Juergen Luethje <j.lue at gmx.de> Jul 17, 2003
- 506 views
Hi Igor, you wrote: > If you have many basic arrays with 0-indexing and want > to translate them into EU, yes, this is a real pain and > a rich source of the subtle bugs, especially when > indexes are calculated. > > I have dropped some my translations from basic into EU > just because of too many confusions in indexes, > especially if some of the 0-elements > are or are not empty. > > Maybe, someone has a powerful tip, > other than just a good attention? Instead of creating, writing, and reading the arrays/sequences directly, you can access them via functions. Of course, you have the full power and flexibility of Euphoria. Here is just an example: -----------------------------[ begin code ]----------------------------- constant LBOUND = 1, UBOUND = 2 global function array_dim (sequence bounds, object initVal) -- create an one-dimensional 'array' with arbitrary bounds -- (even negative indexes are allowed) integer lBound, uBound lBound = bounds[LBOUND] uBound = bounds[UBOUND] if lBound > uBound then return -1 -- error end if return {lBound, uBound, repeat(initVal, uBound-lBound+1)} end function global function array_put (sequence array, integer index, object val) -- write to an one-dimensional 'array' with arbitrary bounds integer lBound lBound = array[LBOUND] if index < lBound or array[UBOUND] < index then return -1 -- error end if array[3][index-lBound+1] = val return array end function global function array_get (sequence array, integer index) -- read from an one-dimensional 'array' with arbitrary bounds integer lBound lBound = array[LBOUND] if index < lBound or array[UBOUND] < index then return -1 -- error end if return array[3][index-lBound+1] end function ---+++ demo +++--- include misc.e sequence who who = array_dim({2000, 2002}, "") puts(1, "Our 'array':\n") pretty_print(1, who, {3}) who = array_put(who, 2000, "Kim Dae-jung") who = array_put(who, 2001, "United Nations, Kofi Annan") who = array_put(who, 2002, "Jimmy Carter") puts(1, "\nRecent Nobel Prices for Peace:\n") for year = 2000 to 2002 do printf(1, "%d: %s\n", {year, array_get(who, year)}) end for ------------------------------[ end code ]------------------------------ In August 2002, there was also a discussion about arrays on this list. Derek provided code for creating (1-based) multi-dimensional 'arrays', that looked like this: -----------------------------[ begin code ]----------------------------- global function dim_array (sequence dimension, object init_value) -- example: sequence a -- a = dim_array({3,7,4}, 0) object array if length(dimension) = 0 then return 0 -- error end if array = init_value for i = length(dimension) to 1 by -1 do if atom(dimension[i]) and dimension[i] > 0 then array = repeat(array, dimension[i]) else return i -- error end if end for return array end function ------------------------------[ end code ]------------------------------ If you combine both approaches, you'll have code for handling multi-dimensional arrays with (almost) arbitrary bounds. Best regards, Juergen -- /"\ ASCII ribbon campain | |\ _,,,---,,_ \ / against HTML in | /,`.-'`' -. ;-;;,_ X e-mail and news, | |,4- ) )-,_..;\ ( `'-' / \ and unneeded MIME | '---''(_/--' `-'\_)