Weblog Commenting and Trackback by HaloScan.com smspoonam@gmail.com: 05/05/07
|

Saturday, May 05, 2007

Associative arrays

Associative arrays, which are sometimes called hashes or maps, use keys instead of a numeric index to organize stored values. Each key in an associative array is a unique string that is used to access a stored value.

There are two ways to create associative arrays in ActionScript 3.0. The first way is to use the Object constructor, which has the key advantage of allowing you to initialize your array with an object literal. An instance of the Object class, also called a generic object, is functionally identical to an associative array. Each property name of the generic object serves as the key that provides access to a stored value.

The following example creates an associative array named monitorInfo, using an object literal to initialize the array with two key and value pairs:

var monitorInfo:Object = {type:"Flat Panel", resolution:"1600 x 1200"};
trace (monitorInfo["type"], monitorInfo["resolution"]);
// output: Flat Panel 1600 x 1200

If you do not need to initialize the array at declaration time, you can use the Object constructor to create the array, as follows:

var monitorInfo:Object = new Object();

After the array is created using either an object literal or the Object class constructor, you can add new values to the array using either the bracket operator ([]) or the dot operator (.). The following example adds two new values to monitorArray:

monitorInfo["aspect ratio"] = "16:10";
// bad form, do not use spacesmonitorInfo.colors = "16.7 million";
trace (monitorInfo["aspect ratio"], monitorInfo.colors);
// output: 16:10 16.7 million

Note that the key named aspect ratio contains a space character. This is possible with the bracket operator, but generates an error if attempted with the dot operator. Using spaces in your key names is not recommended.

The second way to create an associative array is to use the Array constructor and then use either the bracket operator ([]) or the dot operator (.) to add key and value pairs to the array. If you declare your associative array to be of type Array, you cannot use an object literal to initialize the array. The following example creates an associative array named monitorInfo using the Array constructor and adds a key called type and a key called resolution, along with their values:

var monitorInfo:Array = new Array();
monitorInfo["type"] = "Flat Panel";
monitorInfo["resolution"] = "1600 x 1200";
trace (monitorInfo["type"], monitorInfo["resolution"]);
// output: Flat Panel 1600 x 1200

There is no advantage in using the Array constructor to create an associative array. You cannot use the Array.length property or any of the methods of the Array class with associative arrays, even if you use the Array constructor or the Array data type. The use of the Array constructor is best left for the creation of indexed arrays.

|

Curious About Brady?

Macromedia just published a new Logged In article with a lot of great information on Brady, Macromedia's new Flex IDE (which just entered final beta). It contains some pretty detailed descriptions, and several good screenshots. Here's a little taste:

Brady is the Macromedia IDE for Flex application development. Designers and developers can be more productive building Flex applications through tight integration between the IDE and the server. Developers can learn MXML and type code more quickly and accurately using Brady's code hinting feature. More visually-oriented programmers can use the drag-and-drop Design view to quickly lay out Flex interfaces and style them using CSS....Check this

linkhttp://www.adobe.com/devnet/logged_in/hb_williams_brady.html

|

FLEX can change context.

Context is when you look at a situation from a perspective and generally fall into a predefined segment. In that, if I look at say Microsoft Word, my context of use is to read a document - yet others may use word to construct a document. So different roles play different parts, thus the segments of the technology is defined by context.

Adobe FLEX, is one of these products that can reshape and redefine context under the guise of being passive while unbeknown to others, it's quite aggressive in the way it hijacks segments of use. A year ago, I meet up with a fellow-FLEX developer, who showed me approaches to using the language that I guess challenged the way in which I've approached technology. The way in which he did this, was he showed be a before and after approach to the same software, yet one had FLEX and what had HTML. It was pretty much a straight forward deal; on one side you had an ugly HTML User Interface (UI) while on the other you had this rich vibrant UI.

The rich vibrant UI was the easier sell, as lets face it, we do tend to at times judge a book by its cover - sorry, it does happen. Yet, one thing that stood out from the start that I couldn't quite explain to others that he conveyed to me was that the perception was changed or be it, context.

You see, there wasn't just a sexier UI over the top, no, what was really changed was the fact that instead of seeing a HTML List of names, suddenly we were seeing a directory of documents. Yes that's right, documents which suddenly changed folk's perceptions or context of use.

Looking at a HTML list, it's vanilla and doesn't provide you much in terms of interaction. You move your mouse over it, at times there maybe style updates but other then that it's still simply a plain old list. A developer could spice things up, by allowing a context box to hover under the mouse point as one moves over the list item, giving more detailed look into what's behind this list item link which the developer may get some applause for.

Yet, what this co-flex-developer did was change the context of the data's use, by turning a list item into a document. So now, end users could be expected to now approach the exact same data set (being for this example, a persons name and say their personal information) with pre-defined expectations.

The end-user could arguably, click on the document of the persons name and then expect to carry out actions such as copy or print? The end-user could also expect to double click on the document and see a more detailed view of the end user in a report style approach. The list could go on.

So now, the context of use has changed, and it was done by trickery using technology such as FLEX.

Now, comes the AJAX zealots, I see you, sitting in the front row ready to pounce on any mention of how great FLEX is. AJAX can do the above, no question about it, but it could also be more work to get to that point and lets face it, hunting and gathering the data in an efficient timely manner with richer UI that's controlled 100% by the agent in which its viewing across all manners of operating systems - is well... harder then it looks.

To do the above, is basically really small amount of work - in fact the hardest part is deciding whether you want to use SOAP (maybe you do want to appease the AJAX zealots while giving the FLEX hand a try) or AMF as your gateway access to such data.

The overall point I have is that FLEX can really shift context into gear, it can not only provide radical adjustments to the perception of how data will be used within UI, but it can also be used as a bridge or conductor if you will over how disparate backend technologies can be bridled.

It's got an easy approach to getting the visuals out of the way, so one can focus more on data transmissions back and forth. It can work closely with DOM by not only allowing it to be overlayed on top but it can also provide a much faster turn around in injecting packets of HTML into the DOM Hierarchy given via server-side. Injecting DOM via FLEX is simple, while at the same time through E4X, flex provides a faster way to decouple portions of HTML before DOM injection occurs.

So as you can see, not only is FLEX adjusting context of visual elements within software, but it can also adjust context of how data is swapped around through different tiers. It's the filter if you will to all, and this filter has been governed by context.

Context is why FLEX should be used, not because "all the kids are doing it".

For my next pet-project, i'm looking at ways to combine Contribute and FLEX. *shrug* who knows what I can uncover in this unchartered space.