Jump to content

Selecting a range of values


mjlongo

Recommended Posts

Hello, I have a job where the customer supplies 5 fields. F1, F2, F3, F4 and F5. They will either contain a zero or they will contain a numeric value. I have to select the range from the fields and print that range of fields when they are not zero. Fields inside the range can contain a zero. So for example:

 

F1=50, F2=100, F3=200, F4=300, F5=100: I print all fields.
F1=0, F2=100, F3=200, F4=300, F5=100: I print fields F2 through F5.
F1=50, F2=100, F3=0, F4=300, F5=0: I print fields F1 through F4.
F1=0, F2=0, F3=200, F4=0, F5=100: I print fields F3 through F5.
F1=0, F2=0, F3=200, F4=0, F5=0: I print only field F3.

 

I'm pretty sure I can come up with a long list of if statements to handle this but I'm also sure there is a smarter way. Maybe using some kind of test on an array? Any ideas on how to handle this would be greatly appreciated.

Thanks!

Edited by mjlongo
Link to comment
Share on other sites

I wanted to follow up and state that I managed to figure out a pretty simple script to solve this issue. Maybe someone else can benefit from this. I just put the fields into an array and parsed from both the front and back of the array. I replaced any field that contained "$0" with the word "ZERO". This then allowed me to print any field that does not contain the word "ZERO". It turned out to be quite easy and worked perfectly for what I needed to do.

 

Refer to the code below:

 

// Create a results variable
var results='';

//Trim excess spaces from the fields in the data file
var cashLY=Trim(Field("Cash LY"));
var cash2Y=Trim(Field("Cash 2Y"));
var cash3Y=Trim(Field("Cash 3Y"));
var cash4Y=Trim(Field("Cash 4Y"));
var cash5Y=Trim(Field("Cash 5Y"));

// Put each field into an array
var arr = [cashLY,cash2Y,cash3Y,cash4Y,cash5Y]

/* check from front of array then the back for any fields that contain 
"$0". If a field does contain a "$0", replace it with the word "ZERO"
Otherwise, break out of the routine if we find a field that has 
anything other than a "$0". This will result in a middle range. */

// check from front of array
for (i=0; i < arr.length; i++){
   if(arr[i]=='$0'){
       arr[i]='ZERO';
   }else{
       break;
   };
};

// check from back of array
for (i=arr.length-1; i >=0 ; i--){
   if(arr[i]=='$0'){
       arr[i]='ZERO';
   }else{
       break;
   };
};

/* After parsing through the array, we should be left with fields
from the front of the array and then from the back of the array that
now have the word "ZERO" in it.  We can then use that information to
print the range of fields that we have been left with */

if (arr[0]!='ZERO'){results += '2020-2021<t>'+cashLY+'<br>'};
if (arr[1]!='ZERO'){results += '2019-2020<t>'+cash2Y+'<br>'};
if (arr[2]!='ZERO'){results += '2018-2019<t>'+cash3Y+'<br>'};
if (arr[3]!='ZERO'){results += '2017-2018<t>'+cash4Y+'<br>'};
if (arr[4]!='ZERO'){results += '2016-2017<t>'+cash5Y};

return results;   


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...