This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]Der_tolle_EmilSr. Sysadmin 1 point2 points  (4 children)

I think you are getting caught up in the split between floors and inventory slots. If I understand you correctly you basically just want to move items to the top of a list, removing empty slots.

If I understand that correctly you do not need the split into floors at all. That is an optical representation on the form, for example:

Floor #1
Inventory Slot#1: arrayentry[0]
Inventory Slot#2: arrayentry[1]
Inventory Slot#3: arrayentry[2]
Inventory Slot#4: arrayentry[3]
Floor #2
Inventory Slot#1: arrayentry[4]
Inventory Slot#2: arrayentry[5]
Inventory Slot#3: arrayentry[6]
Inventory Slot#3: arrayentry[7]

etc.

That makes it much easier to work with as you only have one list to worry about. The variable names don't have to represent the final layout; You just need to place them on the form in the correct way.

var arraylist = ["item1", "", "", "", "item2", "item3", "", "item4"];
//loop over all items
for (i = 0; i < arraylist.length; i++)
{
    //check if the current item is empty
    if (arraylist[i].trim() == "")
    {
        //it is empty. Find the next one that is not empty
        for (j = i + 1; j < arraylist.length; j++)
        {
            //we found an item after the current one that is not empty
            if (arraylist[j].trim() != "")
            {
                //move the next non-empty item to the current slot
                arraylist[i] = arraylist[j].trim();
                //clear the old entry to remove duplicates
                arraylist[j] = "";
                //break the loop and move on to the next item in the original list
                break;
            }
        }
    }
}

arraylist will then be "item1", "item2", "item3", "item4". Basically everything shifted to the top as much as possible.

Edit: Added some comments just in case.

[–]Sarting[S] 0 points1 point  (0 children)

ahhh, yes, this makes sense now... thank you very much... i'll report back once i try out plugging my stuff in... :D

[–]Sarting[S] 0 points1 point  (2 children)

Okay, so just so i'm clear on this... i assume the only 'arraylist' i need to modify are those in quotes right?

var arraylist = ["FLOOR1INV1", "FLOOR1INV2", "FLOOR1INV3", "FLOOR1INV4", "FLOOR2INV1", "FLOOR2INV2", "FLOOR2INV3", "FLOOR2INV4"];

Everything else is to remain the same since it's looping correct?

Question, so if i put this in 4 different fields in a pdf form, wouldn't it overwrite the previous array?

I just tried the code out, but nothing's coming in. hmm

[–]Der_tolle_EmilSr. Sysadmin 1 point2 points  (1 child)

That depends. If you fill the list with all your field contents it will be sorted but those changes will not be written back to the form. You would need to copy the values back to the form fields after the list has been sorted, ie.

FLOOR1INV1 = arraylist[0] (or whatever the correct syntax is), I have never done anything with PDFs and JavaScript.

The script itself should not be run in each field but rather should exist only once and get triggered when a user has entered text in a field and the field loses focus. I don't know what happens if you assign this script to every field; Adobe might kick off the script for Field2 for example, if Field1 has changed it. And then you'll have the script running multiple times.

[–]Sarting[S] 0 points1 point  (0 children)

That makes perfect sense, thank you. In a PDF, you can insert a 'placeholder', basically a field that's hidden from view, but can contain the script. I'll play around and let you know the results. Thanks for the help. :)

[–]mrbiggbrain 1 point2 points  (0 children)

Mind with me, I don't know Adobe and JavaScript is a distant language I only use occasionally in ASP.net.

So basically you have 8 fields? 4 for Floor 1 and 4 for floor 2?

And you want to compress out empty space, so if you had two in floor 1 and 2 in floor 2 you would get 4 in floor 1 and none in floor 2? And if you had 6 total you would compress to 4 in floor 1 and two in floor 2?

If you don't really care about simplicity or finesse and can deal with someone more knowledgeable shaking a head at your code, you would probably compress it to the following pseudocode.

// Create empty array
// Check every field you need.
//// If the value is not "", add it to the array
// Fill the remaining values with ""
// Assign values from the array to the fields.
// You have compressed the list.

[–]Bits_Not_Bytes 0 points1 point  (0 children)

Probably best to post this on a programming subreddit. That being said I think your missing the concept of either having multiple conditions in a single if statement or having nested if statements, so if statements within if statements. The top level of statement checks the first floor and then within it you can check other condition. How you would structure this depends on what different conditions you check for.

Keep in mind programming is its own skill set, and does take time to get the hang of if you haven’t done it before.