Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently December 22nd, 2024, 9:41 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Compare All Array Values are Identical
PostPosted: January 15th, 2012, 2:21 am 
Offline

Joined: June 2nd, 2010, 11:48 pm
Posts: 10
Opus: Professional v7.01
OS: Windows 7
Hi,

I am populating an array with values from an INI file and I would like to know if there is a way to compare if all these values are the same. The array I am using has 22 values stored in it.

E.g.
Array[0]=true
Array[1]=true
Array[2]=true
Array[3]=true

If all are true then execute further script.

If any one is false then do nothing.

I did find an article on the webdeveloper forum "To check whether all the values in an array are identical"

http://www.webdeveloper.com/forum/showt ... p?t=237860

But being a beginner I don't fully understand what is happening. It appears the reply at the bottom of the page would work yet I'm not sure how to check or implement in Opus Pro V7.06

Does anyone have a suggestion as to how I might go about this?

Thanks.

Andrew


For this message intertag has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: Compare All Array Values are Identical
PostPosted: January 17th, 2012, 12:00 pm 
Offline
Godlike
Godlike

Joined: November 12th, 2005, 1:56 am
Posts: 1474
Location: SFBay Area
Opus: OpusPro v9.0x, & Evol.
OS: Vista32
System: Core 2 duo 2Ghz, RAM 3GB, Nvidia Go 7700 - laptop
Quote:
If all are true then execute further script. If any one is false then do nothing.
If I pay attention only to that issue and totally disregard other parts of the stated problem, then the below may help you solve it. This assumes that the values pulled from the INI file are: true, and some other string. So we only need to test for true. (To include the other part: "...if there is a way to compare if all these values are the same" would be solved a slightly different way.)

Code:
var Control01 = Arrayname1.length ;
var testing1 = 0 ;
for (i=0; i <Control01 ; i++)
{
    if ( Arrayname1[ i ] != true )
    {
    return;
    }
    else
    {
    testing1++
    }
         if ( testing1 == Arrayname1.length )
        {
         //insert your code here to execute further script

      //one way to exit the for loop is to set our controlling variable, Control01, to some value less than i
      Control01 = 0 ;
        }
//at this point, for loop will keep running the test, unless the first IF statement above has already found a value of 'false' or other non-true value.
}

This may not be the most code efficient solution, but should get it done.

_________________
_good things come to those who wait(0)_


For this message Lar_123 has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: Compare All Array Values are Identical
PostPosted: January 17th, 2012, 12:39 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
@lar,

Not really necessary to keep track off all the successes, just look for the first failure:

Code:
for (var i=0;i<myArray.length;i++)
   {
   if (myArray[i] !=true) break
   }

if (i < myArray.length) Debug.trace("values don't match, me thinks!")


Of course, if you don't care about a few extra cycles, then I guess this would do:

Code:
myArray.sort()
if (myArray[0] != myArray[myArray.length-1]) Debug.trace("Out damn false, out!")



Mack

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
 Post subject: Re: Compare All Array Values are Identical
PostPosted: January 17th, 2012, 1:11 pm 
Offline
Godlike
Godlike

Joined: November 12th, 2005, 1:56 am
Posts: 1474
Location: SFBay Area
Opus: OpusPro v9.0x, & Evol.
OS: Vista32
System: Core 2 duo 2Ghz, RAM 3GB, Nvidia Go 7700 - laptop
Quote:
Not really necessary to keep track off all the successes, just look for the first failure:
Thanks Mack. I'm always learning around you. :D
I thought putting 'break' in the IF Statement would exit there, but that the FOR LOOP would keep running. I did not test the scripts. Anyway, yours is much more concise. Good things. I think Andrew still needs to know where to put his code that executes upon success (all true).

_________________
_good things come to those who wait(0)_


For this message Lar_123 has been thanked by : mackavi


Top
 Profile  
 
 Post subject: Re: Compare All Array Values are Identical
PostPosted: January 17th, 2012, 1:35 pm 
Offline
Godlike
Godlike
User avatar

Joined: March 21st, 2007, 10:44 am
Posts: 3188
Location: UK
Opus: Evolution
I always under use breaks. Remind me to much of the good old days of GOTO.

Andrew's fine. He'd already emailed me the question as it related to something I'd written for him.

Mack

_________________
When you have explored all avenues of possibilities, what ever remains, how ever improbable, must be the answer.

Interactive Solutions for Business & Education
Learn Anywhere. Learn Anytime.

www.interaktiv.co.uk
+44 (0) 1395 548057


Top
 Profile Visit website  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 60 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group