[thelist] Important admin note re: evolt.org (LONG. Please read.) (was: zipcodes)

.jeff jeff at members.evolt.org
Tue Mar 11 15:21:30 CST 2003


john,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: John Handelaar
>
> > PS From http://evolt.org if I click on "tips" it
> > redirects me to the list subscription page. I don't
> > think that's intentional.
> >
> > PPS http://lists.evolt.org/index.php?content=harvest
> > Does not have any information about tip harvesting
> > other than a link at the bottom to the URL I just
> > pasted.
>
> Wondered how long it'd take someone to notice...
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

and almost as quick as it's pointed at, it's fixed.

<tip type="ColdFusion" author=".jeff">

have a bunch of numbered items that you want to store as indexes in an
array, but there are gaps?  consider the following example data:

19	Emu
1	Monkey
53	Western Diamondback
2	Elephant
31	Platypus
5	Aardvark

you have two options.  if the order of the indexes doesn't matter, the first
(and preferred, imo) is to use the indexes as keys in a structure (the
quotes around the number is very important):

<cfscript>
  animals = StructNew();
  animals['19'] = 'Emu';
  animals['1'] = 'Monkey';
  animals['53'] = 'Western Diamondback';
  animals['2'] = 'Elephant';
  animals['31'] = 'Platypus';
  animals['5'] = 'Aardvark';
</cfscript>

depending on the version of coldfusion server you're using pre-mx or mx the
order these keys will be output in a for/in or collection loop will vary.
pre-mx will be output in dictionary order -- 1,19,2,31,5,53.  mx will be
output in the order you created the keys -- 19,1,53,2,31,5 -- unless you
manually sort the list of struct keys first and use that list to output the
key values, like so:

<cfscript>
  keysArray = StructKeyArray(animals);
  ArraySort(keysArray, 'numeric');
</cfscript>
<cfloop from="1" to="#ArrayLen(keysArray)#" index="animal">
  animals['#keysArray[animal]#'] = '#animals[keysArray[animal]]#';<br />
</cfloop>

swap out numeric for text or textnocase as the second argument of the
ArraySort() function and you'll get identical results on mx as pre-mx.

your second option is to use an array of structures where the numeric value
is stored as a key of the appropriate array element, like so:

<cfscript>
  animals = ArrayNew(1);
  animals[1] = StructNew();
  animals[1].index = 19;
  animals[1].value = 'Emu';
  animals[1] = StructNew();
  animals[2].index = 1;
  animals[2].value = 'Monkey';
  animals[3] = StructNew();
  animals[3].index = 53;
  animals[3].value = 'Western Diamondback';
  animals[4] = StructNew();
  animals[4].index = 2;
  animals[4].value = 'Elephant';
  animals[5] = StructNew();
  animals[5].index = 31;
  animals[5].value = 'Platypus';
  animals[6] = StructNew();
  animals[6].index = 5;
  animals[6].value = 'Aardvark';
</cfscript>

now the order is intact, but the output is quite a bit more complex.

you're probably asking yourself why you can't just do it like this:

<cfscript>
  animals = ArrayNew(1);
  animals[19] = 'Emu';
  animals[1] = 'Monkey';
  animals[53] = 'Western Diamondback';
  animals[2] = 'Elephant';
  animals[31] = 'Platypus';
  animals[5] = 'Aardvark';
</cfscript>

well, it's simple.  when you create individual indexed items in an array,
coldfusion doesn't fill in the gaps for you.  so, even though the array will
show a length of 53, it'll throw an error when looping from 1 to 53 saying
that "The element at position 3 in dimension 1 of object "animals" cannot be
found. The object has elements in positions 1 through 53. Please, modify the
index expression.".

unfortunately there isn't a way in pre-mx versions to check if an index
exists before trying to output it.  if you're using mx, then there's two
udfs at cflib.org that'll help you out -- ArrayDefinedAt() that'll let you
do a check before trying to output it and IsSafeArray() that checks an
entire array for gaps.  ultimately, you're better off avoiding the creation
of arrays that could have gaps.

</tip>

thanks,

.jeff

http://evolt.org/

NOTICE:  members.evolt.org web and email address are changing!
---------------------------------------------------------------------
| OLD:                            | NEW:                            |
| jeff at members.evolt.org          | evolt at jeffhowden.com            |
| http://members.evolt.org/jeff/  | http://evolt.jeffhowden.com/    |
---------------------------------------------------------------------





More information about the thelist mailing list