Re: Array manipulation question



DL schreef:
say, we have the following:

// declare an array
myArray = []; // short hand? same as myArray = new Array ?

// populate it
myArray[0] = 0;
myArray[1] = 0;
myArray[2] = 1;
myArray[3] = 0;

// now problem to solve
// fact: the above array has 4 sets of data, namely 3 zeros and 1 of
value one.

Hi,

Well, they do not contain a 'set' but just a plain value.

// say, the business rule is, if all set of data are of same value ( 0
|| 1), then do nothing
else alert ('hey dark sheep found').

I understand Array has a bunch of methods, but not sure which one is a
good one to solve the above problem.

No offense, but this is so basic I advise you to buy a book on JavaScript (any will do, but O'Reilly's Definitive Guide is good).

Here follows an example to get you going.
(It can be written in many different ways, and shorter, but I hope this version is clear to you.)

<script type="text/javascript">
var myArray = [];
// populate it
myArray[0] = 0;
myArray[1] = 1;
myArray[2] = 0;
myArray[3] = 0;

var arrLength = myArray.length;
var firstVal = myArray[0];

for(var i=0;i<arrLength;i++){
if (myArray[i] != firstVal){
alert ("Black sheep found.");
break;
}
}
</script>


Thanks.

You're welcome.

Regards,
Erwin Moller
.



Relevant Pages

  • Re: declaring a public constant?
    ... Sub AutoExec() ... The declaration of the array has to be before the first routine in the module. ... The code to populate the array must be in the AutoExec subroutine, which is run automatically when the add-in is loaded. ... If you declare the array as Public, it will be available to all modules in your add-in, if you declare it as Private, it will only be available to routines in the same module. ...
    (microsoft.public.word.vba.general)
  • Re: Populate a Multi-Column ListBox
    ... It seems to me that I should somehow be able to directly populate ... ListBox column 1 with an array of state names created using the Split ... Dim myArray1 As Variant ...
    (microsoft.public.word.vba.userforms)
  • Re: Populate a Multi-Column ListBox
    ... It seems to me that I should somehow be able to directly populate ... ListBox column 1 with an array of state names created using the Split ... Dim myArray1 As Variant ...
    (microsoft.public.word.vba.userforms)
  • Re: Populate a Multi-Column ListBox
    ... two-dimensional array to load into a ListBox or ComboBox. ... For a multicolumn ListBox or ComboBox, AddItem inserts an entire row, ... If you populate a ListBox with an Array using the List of Column ... Dim myArray As Variant ...
    (microsoft.public.word.vba.userforms)
  • Re: Populate a Multi-Column ListBox
    ... two-dimensional array to load into a ListBox or ComboBox. ... For a multicolumn ListBox or ComboBox, AddItem inserts an entire row, ... If you populate a ListBox with an Array using the List of Column ... Dim myArray As Variant ...
    (microsoft.public.word.vba.userforms)