Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently November 7th, 2024, 11:18 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Function
PostPosted: August 11th, 2007, 7:44 pm 
Offline

Joined: March 8th, 2005, 9:40 pm
Posts: 63
Hi
I am trying to write a function that takes the contents of a variable, in which all the fields are separated by a comma, this case Kpress.

Kpress will look something like this: 0,1,0,2,0,3,0,4,0,5

I want this to be split in to several variables.
item1 will consist of the first two elements of Kpress i.e 01
item2 will consist of the next two elements of Kpress i.e 02
etc.

Here is my attempt:

function ItemC(Kpress,item_num,item1,item2,item3,item4,item5)
{
var itemdesc = new String(Kpress);
var myArray = itemdesc.split(",");
var item1 = myArray[0]+ myArray[1];
var item2 = myArray[2]+ myArray[3];
var item3 = myArray[4]+ myArray[5];
var item4 = myArray[6]+ myArray[7];
var item5 = myArray[8]+ myArray[9];
var itemArray=(item1,item2,item3,item4,item5)
}

The last line is to create a new array or a new variable that would look like this:
01,02,03,04,05

But it does not seem to work, what have I done wrong?

Regards

Neil

_________________
Opus Pro V6
Dell Inspiron 9400
Intel Centrino Duo 2GHz
2 Gig Ram
XP SP2


Top
 Profile  
 
 Post subject:
PostPosted: August 11th, 2007, 9:31 pm 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
Hi Nitro,
This post should be in the advanced section :)
You haven't declared itemArray properly; use new Array().
drop this into a script and hit F5.

//##################################
Kpress = "0,1,0,2,0,3,0,4,0,5"

var itemdesc = new String(Kpress);

var myArray = itemdesc.split(",");

var item1 = myArray[0]+ myArray[1];
var item2 = myArray[2]+ myArray[3];
var item3 = myArray[4]+ myArray[5];
var item4 = myArray[6]+ myArray[7];
var item5 = myArray[8]+ myArray[9];

var itemArray=new Array(item1,item2,item3,item4,item5);

Debug.trace("itemArray = "+itemArray+"\n")

//##################################

Paul


Top
 Profile  
 
 Post subject: for loop...
PostPosted: August 12th, 2007, 11:45 am 
Offline

Joined: March 8th, 2005, 9:40 pm
Posts: 63
Hi Paul,

Thanks, it works perfectly! :D

I the only problem now is that if the user does not fill Kpress completely I get the following:

if Kpress only has the first two elements i.e 0,1
and is not complete ie 0,1,0,2,0,3,0,4
item1 = 01
item2 = undefined
item3 = -1.#IND
item4 = -1.#IND

I think that a for loop or similar is necessary, I am just not sure how to do this.

Regards

Neil

_________________
Opus Pro V6
Dell Inspiron 9400
Intel Centrino Duo 2GHz
2 Gig Ram
XP SP2


Top
 Profile  
 
 Post subject:
PostPosted: August 12th, 2007, 9:12 pm 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
Hi Nitro,
If you allow users to type in something, you will have to trap any errors and inform the user (eg "Sorry, not in the correct format") BEFORE the string is processed.
Error trapping with user input can be quite complex.
The process I use for situations like this is::
I ask a lot of "what if" questions like these before letting a user input data that is going to be processed in some way :-)
if the user puts the commas into the string, do I need to trap things like multiple commas ",," and ",,,"?
Does the string have a fixed length? -- if so I would check the string length for errors.
What happens if they put a 3 or 1 digit number instead of a 2 digit number?
What happens if they put in a decimal point instead of a comma? -- the two keys are close together.

Users will stick anything in an input box -- even by accident, anyone can hit the wrong key.

Another way often used is give them a set of buttons onscreen that contain the number combinations you want them to use, and block keyboard entry.

cheers
Paul


Top
 Profile  
 
 Post subject: Input
PostPosted: August 12th, 2007, 11:55 pm 
Offline

Joined: March 8th, 2005, 9:40 pm
Posts: 63
Hi Paul,

All the input is via left click on buttons. (button0 to button9)

To get the string of Kpress: 0,1,0,2,0,3,0,4,0,5, etc.
The user would have to click
on button0
then button1
then button0
then button2
etc.

Imagine testing someone on using an ATM on a simulated ATM graphic interface with buttons.

My problem is as this is a simulation test, I am concerned that the user may not complete the sequence, and create problems in the program because of this.

Kpress can consist of up to 36 elements, but the user is only required, at this stage to complete the first 18 elements. If the user completes all 36 elements, this will not create a problem as the remaining 18 elements will be ignored.

I am not concerned about the user clicking on the incorrect button sequence as they will be marked according to their input.

I must say that I am impressed with the power of Opus, in that I have managed to accomplish what I have so far with minimal scripting.

Even if my efforts have been clumsy :oops: so far I have managed to get quite far in achieving my goal.

When I have struggled, unpaid members of this forum have helped me despite my ignorance.

For that I thank you.

Regards

Neil

_________________
Opus Pro V6
Dell Inspiron 9400
Intel Centrino Duo 2GHz
2 Gig Ram
XP SP2


Top
 Profile  
 
 Post subject:
PostPosted: August 13th, 2007, 4:06 am 
Offline

Joined: November 11th, 2004, 4:05 am
Posts: 636
Location: Christchurch, NZ
If the user does not fill Kpress properly, you will have to make Opus stop and tell him/her.
In this case the string length is 19, and that can be measured with Kpress.length.
If the string length is NOT 19, you can give the user a message to correct things before they are submitted.
See this script for ideas. run it, then change the length of the Kpress string and run it again.
//############################
Kpress = "0,1,0,2,0,3,0,4,0,5"
len = Kpress.length

if(len==19){
var itemdesc = new String(Kpress);
var myArray = itemdesc.split(",");
var item1 = myArray[0]+ myArray[1];
var item2 = myArray[2]+ myArray[3];
var item3 = myArray[4]+ myArray[5];
var item4 = myArray[6]+ myArray[7];
var item5 = myArray[8]+ myArray[9];

var itemArray=new Array(item1,item2,item3,item4,item5);

Debug.trace("itemArray = "+itemArray+"\n")
}
else
{
Debug.trace("please correct your input")
}
//############################

Paul


Top
 Profile  
 
 Post subject: Thanks
PostPosted: August 13th, 2007, 7:31 pm 
Offline

Joined: March 8th, 2005, 9:40 pm
Posts: 63
Hi Paul,

Thanks for all the help, I will play around and let you know how it works.

Regards

Neil

_________________
Opus Pro V6
Dell Inspiron 9400
Intel Centrino Duo 2GHz
2 Gig Ram
XP SP2


Top
 Profile  
 
 Post subject:
PostPosted: August 13th, 2007, 9:06 pm 
Offline

Joined: March 8th, 2005, 9:40 pm
Posts: 63
Hi Paul,

Got it working the way I wanted it to, Its a bit long winded, but it works!

Thanks again for the help. :D

Regards

Neil


Code:
Kpress = "0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,"
len = Kpress.length
Debug.trace(len+"\n")

var itemdesc = new String(Kpress);
var myArray = itemdesc.split(",");

    if(len < 3)
    var item1 = "00";
    var item2 = "00";
    var item3 = "00";
    var item4 = "00";
    var item5 = "00";
    var item6 = "00";
    var item7 = "00";
    var item8 = "00";
    var item9 = "00";

//1
    if((len >= 3 )&&(len < 7)){
    var item1 = myArray[0]+ myArray[1];
    var item2 = "00";
    var item3 = "00";
    var item4 = "00";
    var item5 = "00";
    var item6 = "00";
    var item7 = "00";
    var item8 = "00";
    var item9 = "00";
    }
//2
    if((len >= 7 )&&(len < 11)){
    var item1 = myArray[0]+ myArray[1];
    var item2 = myArray[2]+ myArray[3];
    var item3 = "00";
    var item4 = "00";
    var item5 = "00";
    var item6 = "00";
    var item7 = "00";
    var item8 = "00";
    var item9 = "00";
    }
//3
   if((len >= 11 )&&(len < 15)){
    var item1 = myArray[0]+ myArray[1];
    var item2 = myArray[2]+ myArray[3];
    var item3 = myArray[4]+ myArray[5];
    var item4 = "00";
    var item5 = "00";
    var item6 = "00";
    var item7 = "00";
    var item8 = "00";
    var item9 = "00";
    }
//4
    if((len >= 15 )&&(len < 19)){
    var item1 = myArray[0]+ myArray[1];
    var item2 = myArray[2]+ myArray[3];
    var item3 = myArray[4]+ myArray[5];
    var item4 = myArray[6]+ myArray[7];
    var item5 = "00";
    var item6 = "00";
    var item7 = "00";
    var item8 = "00";
    var item9 = "00";
    }
//5
    if((len >= 19 )&&(len < 23)){
    var item1 = myArray[0]+ myArray[1];
    var item2 = myArray[2]+ myArray[3];
    var item3 = myArray[4]+ myArray[5];
    var item4 = myArray[6]+ myArray[7];
    var item5 = myArray[8]+ myArray[9];
    var item6 = "00";
    var item7 = "00";
    var item8 = "00";
    var item9 = "00";
    }
//6
    if((len >= 23 )&&(len < 27)){
    var item1 = myArray[0]+ myArray[1];
    var item2 = myArray[2]+ myArray[3];
    var item3 = myArray[4]+ myArray[5];
    var item4 = myArray[6]+ myArray[7];
    var item5 = myArray[8]+ myArray[9];
    var item6 = myArray[10]+ myArray[11];
    var item7 = "00";
    var item8 = "00";
    var item9 = "00";
    }
//7
    if((len >= 27 )&&(len < 31)){
    var item1 = myArray[0]+ myArray[1];
    var item2 = myArray[2]+ myArray[3];
    var item3 = myArray[4]+ myArray[5];
    var item4 = myArray[6]+ myArray[7];
    var item5 = myArray[8]+ myArray[9];
    var item6 = myArray[10]+ myArray[11];
    var item7 = myArray[12]+ myArray[13];
    var item8 = "00";
    var item9 = "00";
    }
//8
    if((len >= 31 )&&(len < 35)){
    var item1 = myArray[0]+ myArray[1];
    var item2 = myArray[2]+ myArray[3];
    var item3 = myArray[4]+ myArray[5];
    var item4 = myArray[6]+ myArray[7];
    var item5 = myArray[8]+ myArray[9];
    var item6 = myArray[10]+ myArray[11];
    var item7 = myArray[12]+ myArray[13];
    var item8 = myArray[14]+ myArray[15];
    var item9 = "00";
    }
//9
    if(len >= 35){
    var item1 = myArray[0]+ myArray[1];
    var item2 = myArray[2]+ myArray[3];
    var item3 = myArray[4]+ myArray[5];
    var item4 = myArray[6]+ myArray[7];
    var item5 = myArray[8]+ myArray[9];
    var item6 = myArray[10]+ myArray[11];
    var item7 = myArray[12]+ myArray[13];
    var item8 = myArray[14]+ myArray[15];
    var item9 = myArray[16]+ myArray[17];
    }

var itemArray=new Array(item1,item2,item3,item4,item5,item6,item7,item8,item9);

Debug.trace("one   "+item1+"\n")
Debug.trace("two   "+item2+"\n")
Debug.trace("three "+item3+"\n")
Debug.trace("four  "+item4+"\n")
Debug.trace("five  "+item5+"\n")
Debug.trace("six   "+item6+"\n")
Debug.trace("seven "+item7+"\n")
Debug.trace("eight "+item8+"\n")

Debug.trace("itemArray = "+itemArray+"\n")



_________________
Opus Pro V6
Dell Inspiron 9400
Intel Centrino Duo 2GHz
2 Gig Ram
XP SP2


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 8 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group