Jump to content
Macro Express Forums

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Here's the requirement along with the snippet.

 

  1. Input box to accept value from user till user enter 0 to exit the loop.
  2. The code will check if value >0 then store it in a Array variable.
  3. 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

  1. Input box to accept value from user till user enter 0 to exit the loop.
  2. The code will check if value >0 then store it in a Array variable.
  3. 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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 1
Repeat 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: Result
End Repeat

or can send me the macro on rocki_thegreat@yahoo.com

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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%

Link to comment
Share on other sites

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.

 

hw8vaJy.jpg

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

post-19347-0-92151900-1408262981_thumb.jpg

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.

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.

Loading...
×
×
  • Create New...