On Hash Arrays

Of course XML is great. But there are way older, by far superior TLA mark-up techniques. Hereby a definition of different forms of hash1) lists (aka hash arrays).

Method Example Remarks
Hash terminated 1#2#3#3# De C language is also terminated (but this is done with a semicolon)
Hash separated 1#2#3#4 CSS (and Pascal) is also separated (but not with a hash sign)
Hash enclosed #1#2#3#4# This wiki uses the Pipe sign for enclosing table-cells

For list-use the 3rd form is the best choice. At most points, the exact form doesn’t matter, until you need to check for containment of an item in a list: "#3#9#11#".indexOf("#1#")

For a complete definition, two cases need to be addressed. An empty list should be encoded as an empty string. The explanation: with a list of zero elements, you can’t terminate, separate or enclose an element, so no hash-signs need to be included. Finally, there is no need to define an escape mechanism. Hash arrays are solely intended for numeric data (ID fields in a database for example), so there will be no conflicts.

Example code:

var s, a=[1,2,3,4,5];
//Hash terminated 
s=a.length==0?"":a.join("#")+"#";
a=s.split('#').slice(0,-1);
//Hash separated
s=a.join("#");
a=s.split('#');
//Hash enclosed
s=a.length==0?"":"#"+a.join("#")+"#";
a=s.split('#').slice(1,-1);
//Check
if(a.length!=5||a[0]!=1||a[1]!=2||a[2]!=3||a[3]!=4||a[4]!=5) alert("Something went wrong"); 
else if(!confirm("Did you expect another popup ;-)")) alert("Yeah, come on, what a trust...");
with input from Jan Tuitman
1) The Number_sign is also called pound or hash sign
 
blog\on_hash_arrays.txt · Last modified: 2008-07-23T12:17
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki