23 April 2007

array functions tutorial

Recently I needed some arrays search functions for better performance so I wrote few of them. First one is simple.

function search(wor, arr){
var exist = false;
for(i=0; i< arr.length; i=i+1){
if(arr[i ]==wor){
trace("Element exist on position "+i);
exist = true;
}
}
if(!(exist)){
trace("Element don't exist in array.");
}
}

In for loop we ask if wor is identical to any of array's elements. Note that we use == not = (common mistake). Boolean variable exists is responsible for finding our word in array. If word multiple times in array, all positions will be find. Second function is almost the same. Difference is that this one returns true or false. True if wor is in arr, false otherwise. It goes like this.

function searchB(wor, arr){
var exist = false;
for(i=0; i< arr.length; i=i+1){
if(arr[i]==wor){
return true;
exist = true;
}
}
if(!(exist)){
return false;
}
}

This function will end on first appearence of wor in arr. If wor don't exist in arr, function will return false. You can test these functions like this:

_root.onMouseDown=function(){
planets = new Array("Merkur", "Venus", "Earth", "Mars");
habitable = "Earth";
what = search(habitable, planets);
trace(what);
}

Similar for searchB function.

Third function is called searchCount, simply beacuse it counts number of wor in array. It goes something like this:

function searchCount(wor, arr){
var count = 0;
for(i=0; i< arr.length; i=i+1){
if(arr[i ]==wor){
count += 1;
}
}
return count;
}

Another function is union. If you have two arrays, union will return new array which consist of word that exists in both arrays.

function union(arr1, arr2){ var arr3 = new Array();
count = 0;
for(i=0; i< arr1.length; i=i+1){
for(j=0; j< arr2.length; j=j+1){
if(arr1[i ]== arr2[ j]){
arr3[count ] = arr1[ i];
count+=1;
}
}
}
return arr3;
}

One more important thing. Data type in these functions do not have to be String, it can be any type. For more strict evaluation, you can put === instead of ==.

note: this post have been moved from flanture.blog.com

hello world

Privacy Policy for flanture.blogspot.com


The privacy of our visitors to flanture.blogspot.com is important to us.

At flanture.blogspot.com, we recognize that privacy of your personal information is important. Here is information on what types of personal information we receive and collect when you use and visit flanture.blogspot.com, and how we safeguard your information. We never sell your personal information to third parties.

Log Files
As with most other websites, we collect and use the data contained in log files. The information in the log files include your IP (internet protocol) address, your ISP (internet service provider, such as AOL or Shaw Cable), the browser you used to visit our site (such as Internet Explorer or Firefox), the time you visited our site and which pages you visited throughout our site.

Cookies and Web Beacons
We do use cookies to store information, such as your personal preferences when you visit our site. This could include only showing you a popup once in your visit, or the ability to login to some of our features, such as forums.

We also use third party advertisements on flanture.blogspot.com to support our site. Some of these advertisers may use technology such as cookies and web beacons when they advertise on our site, which will also send these advertisers (such as Google through the Google AdSense program) information including your IP address, your ISP , the browser you used to visit our site, and in some cases, whether you have Flash installed. This is generally used for geotargeting purposes (showing New York real estate ads to someone in New York, for example) or showing certain ads based on specific sites visited (such as showing cooking ads to someone who frequents cooking sites).


DoubleClick DART cookies
We also may use DART cookies for ad serving through Google’s DoubleClick, which places a cookie on your computer when you are browsing the web and visit a site using DoubleClick advertising (including some Google AdSense advertisements). This cookie is used to serve ads specific to you and your interests (”interest based targeting”). The ads served will be targeted based on your previous browsing history (For example, if you have been viewing sites about visiting Las Vegas, you may see Las Vegas hotel advertisements when viewing a non-related site, such as on a site about hockey). DART uses “non personally identifiable information”. It does NOT track personal information about you, such as your name, email address, physical address, telephone number, social security numbers, bank account numbers or credit card numbers. You can opt-out of this ad serving on all sites using this advertising by visiting http://www.doubleclick.com/privacy/dart_adserving.aspx

You can choose to disable or selectively turn off our cookies or third-party cookies in your browser settings, or by managing preferences in programs such as Norton Internet Security. However, this can affect how you are able to interact with our site as well as other websites. This could include the inability to login to services or programs, such as logging into forums or accounts.

Deleting cookies does not mean you are permanently opted out of any advertising program. Unless you have settings that disallow cookies, the next time you visit a site running the advertisements, a new cookie will be added.

*_*

 

template by blogger templates