gagangoomer Posted August 1, 2014 Report Share Posted August 1, 2014 Hi, Can someone share an example of dynamic array? I need to accept value from user and store it in an array which can be used later on. The user will enter '0' to end the loop else it will keep on storing the values in an array variable. Thanks in Advance. Quote Link to comment Share on other sites More sharing options...
Cory Posted August 1, 2014 Report Share Posted August 1, 2014 MEP does not support dynamic arrays. You can only dimension the array at design time. IE a fixed number of elements. FTR I'm not a big fan of this limitation. Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 4, 2014 Author Report Share Posted August 4, 2014 Thanks Cory. I need to accept value from user and store it in an array variable. The no of values can vary for every case, so whenever user enters zero the array will stop and the loop will exit. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Cory Posted August 5, 2014 Report Share Posted August 5, 2014 I use arrays often. I simply make the array dimensions larger than I will ever need.I also have several macro subroutines that give me the same capability of real programs. EG Count, sort, purge, and so forth. Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 6, 2014 Author Report Share Posted August 6, 2014 Thanks Cory, Could you please share an example of array's in variables with me ? Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 7, 2014 Author Report Share Posted August 7, 2014 how to capture values in a variable till the user enter "0". and ow to display those values ? Quote Link to comment Share on other sites More sharing options...
duchi Posted August 9, 2014 Report Share Posted August 9, 2014 not sure how you're requesting to 'capture' values in your scenario, but using the 'Prompt' for value command should work. the loop would continue to log each value from the prompt into your array until a ZERO is entered which a conditional statement can catch and exit the loop. or if you can highlight a value then the 'Pause' command can be utilized. i believe you would assign a variable to the array variable (almost a variable within a variable) and increment it within the array so that you'd have unique variables and not re-use the same variable over and over and ending up with just the last one after the loop is ended. using the array variables at that point to display is almost limitless... use the Message Box, notepad, excel, word, email, etc etc etc Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 11, 2014 Author Report Share Posted August 11, 2014 Thanks Duchi, Do you have a small example for array which you would like to share ? Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 11, 2014 Author Report Share Posted August 11, 2014 Here's the requirement along with the snippet. Input box to accept value from user till user enter 0 to exit the loop. The code will check if value >0 then store it in a Array variable. I’m not sure once the value is stored in an array how to display those values. Below is the snippet of the code for your reference :- Variable Set Integer %N[1]%: Prompt // How many times the loop will run Variable Set Integer %N[2]% to 1 // Initialize If Variable %N[1]% Is Greater Than "0" Repeat Start (Repeat %N[1]% times) Variable Set Integer %Line_No[%N[2]%]%: Prompt // Store value in Array Variable If Variable %Line_No[%N[2]%]% Does not Equal "0" Text Box Display: Variable Modify Integer %N[2]%: Increment End Repeat Text Box Display: Else MessageBox: Incorrect Value End If Input box to accept value from user till user enter 0 to exit the loop. The code will check if value >0 then store it in a Array variable. I’m not sure once the value is stored in an array how to display those values. Below is the snippet of the code for your reference :- Variable Set Integer %N[1]%: Prompt // How many times the loop will run Variable Set Integer %N[2]% to 1 // Initialize If Variable %N[1]% Is Greater Than "0" Repeat Start (Repeat %N[1]% times) Variable Set Integer %Line_No[%N[2]%]%: Prompt // Store value in Array Variable If Variable %Line_No[%N[2]%]% Does not Equal "0" Text Box Display: Variable Modify Integer %N[2]%: Increment End Repeat Text Box Display: Else MessageBox: Incorrect Value End If Quote Link to comment Share on other sites More sharing options...
duchi Posted August 11, 2014 Report Share Posted August 11, 2014 looks like you're on the right track...here's the way i see it, i would flip the logic around a bit. eliminate the prompt for the number of times to loop and just wait for a ZERO; since as you previously stated, the case for the number of entries can vary so why enter it if you already know how to exit?... so you could start with: 1: Variable Set Integer %array_number% to 1 2: Repeat Start (Repeat 1000 times) 3: Variable Set Integer %number%: Prompt // ENTER INTEGER 4: If Variable %number% Equals "0" 5: Repeat Exit 6: End If 7: Variable Set Integer %new_number[%array_number%]% to %number% 8: Variable Modify Integer %array_number%: Increment 9: End Repeat use the variable values wherever you need them... new_number[1] new_number[2] new_number[3] etc etc Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 12, 2014 Author Report Share Posted August 12, 2014 Thanks Duchi.. I ran the above code of lines but didn't get any output. The prompt takes the value but i'm not able to print it. Also how will i get to know the length of array so that i can print a particular value from the array. Note : Please use the Copy Command Text option by selecting the code and right clicking on it. By this way the script can be copied easily by anyone and paste in their MEP as a script instead of text.. Variable Set Integer %array_number% to 1Repeat Start (Repeat 100 times) Variable Set Integer %number%: Prompt If Variable %number% Equals "0" MessageBox: Test Repeat Exit End If Variable Set Integer %new_number[%array_number%]% to %number% Variable Modify Integer %array_number%: Increment MessageBox: ResultEnd Repeat or can send me the macro on rocki_thegreat@yahoo.com Quote Link to comment Share on other sites More sharing options...
duchi Posted August 12, 2014 Report Share Posted August 12, 2014 The last part of my post stated that you'd end up with this series of variables: new_number[1] new_number[2] new_number[3] etc etc You could enter those variables into a msg box (or wherever you need them) to see what they contain. Quote Link to comment Share on other sites More sharing options...
duchi Posted August 12, 2014 Report Share Posted August 12, 2014 oh and to get the length of the array, use the 'store counter in variable' option in the 'repeat start' command and give it a name like array_length. it will include the last zero typed to exit the loop so you can use a modify integer rountine to subtract 1 from the total length if you need to be exact. TextBoxDisplay: array_length new_number[1] new_number[2] new_number[3] etc etc Quote Link to comment Share on other sites More sharing options...
duchi Posted August 12, 2014 Report Share Posted August 12, 2014 i've never used it before but if you use text variables for the array instead of integers in the script, you could look into the 'join string' command and dump the entire contents of the array without typing out every variable which is especially helpful if you're gonna have more than a few. or use another loop to store into a text file or the registry or other clever ways you can come up with. edit: ok just for fun i tried the join string function and it works beautifully! Variable Set Integer %array_number% to 1 Repeat Start (Repeat 100 times) Variable Set String %OriginalString%: Prompt If Variable %OriginalString% Equals "0" Repeat Exit End If Variable Set String %StringArray[%array_number%]% to "%OriginalString%" Variable Modify Integer %array_number%: Increment End Repeat Variable Modify Integer: %array_length% = %array_length% - 1 Join String %StringArray%[1..100] with " " into %StringJoined% Text Box Display: array length: %array_length% array elements: %StringArray[1]% %StringArray[2]% %StringArray[3]% total array with one space in between: %StringJoined% Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 14, 2014 Author Report Share Posted August 14, 2014 Thanks Duchi, But i'm getting the below error message (screenshot attached) from the above code. Quote Link to comment Share on other sites More sharing options...
duchi Posted August 14, 2014 Report Share Posted August 14, 2014 You have to add new variables to your list of variables by double clicking the line in the script that contains a variable which hasn't been defined. That's the only way I know of to do it other than manually adding a variable to the list. In other words, if you copy and paste a script from another macro (or this forum) that contains a new variable, you have to define it. I thought the latest update of mxp did that automatically but obviously sometimes it doesn't pull them over. be sure to do the above for every line that has undefined variables or you will get the same error msg after each run of the script until all variables have been cinfirmed. Thanks Duchi, But i'm getting the below error message (screenshot attached) from the above code. Quote Link to comment Share on other sites More sharing options...
Samrae Posted August 15, 2014 Report Share Posted August 15, 2014 gagangoomer: When you copied the macro and then saved, Macro Express Pro offered to create each variable in turn. When the variable %StringArray% was created did you make sure that it was defined as a string array variable? To fix it, open the macro in the Script Editor. Click the Variables tab. Click on the [+] next to Text variables. Look for StringArray. Double-click on it. Make sure "Create as an array" is clicked. Make sure the number of elements is high enough. Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 16, 2014 Author Report Share Posted August 16, 2014 I tried the steps you mentioned above, but still its not working for me....Can you please send me the macro you created for me ? I tried a lot of options but none is working for me. Quote Link to comment Share on other sites More sharing options...
duchi Posted August 17, 2014 Report Share Posted August 17, 2014 Like Samrae stated, review the details of the variable. Click the variables tab in the script and see if the variable you are confirming is in the list. Create it if it isn't... review the details and modify them as appropriate. Try not to look at it until you've exhausted the above, but I've included a screen print of how the properties of the StringArray variable should look. Try using names which you've created to keep things simple and understood by you. It seems you may be getting hung up on the concept of creating and naming variables perhaps? I'm confused because in post #9 you referenced variables used correctly...or did another user write that for you? Keep trying, you gotta understand this concept of creating and maintaining variables in an effort to create robust and intuitive scripts on your own. Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 18, 2014 Author Report Share Posted August 18, 2014 Double checked the Varibale. It is defiened as an array variable still giving the same error message. I tried to copy the code as you mentioned above but it gets copied as text type and not the macro script...not sure why it is acting so wired. Quote Link to comment Share on other sites More sharing options...
duchi Posted August 18, 2014 Report Share Posted August 18, 2014 Try the attachment. IntegerLoop.mex Quote Link to comment Share on other sites More sharing options...
gagangoomer Posted August 21, 2014 Author Report Share Posted August 21, 2014 Thanks Duchi & Samrae , Its working now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.