Jump to content
Macro Express Forums

Extract random data/lines from text tile and append to a text string


Recommended Posts

Hello all,

I have a rather simple task; however, I'm new to MEP and could not find a specific reference to this need within help in the program, the first 15 pages of this forum or anywhere else :(. Just need a nudge in the right direction for which commands/variables to use...

 

I need to extract a random value from a text file with 100 or so entries (which will vary) and append it to a text string I want to use for a separate purpose. The text file contains all integers that range from 1 to 10000 (and will change on a regular basis). Say the first 5 lines of the file are 37, 112, 156, 256, 423. There are 100+ such entries. I need to pull repeadly one value at random constantly changing and append it to a string. Text file process? Ugh. Thanks in advance for any guidance you could provide :).

Link to comment
Share on other sites

Hi Jax and welcome to the forum.

 

If you want to avoid the Text/ASCII File Processing commands for the time being, then here's one approach. It's fairly crude but hopefully its logic is clear enough for you to build on.

 

There are some vague aspects of your description so note my embedded assumptions. In particular note the change you'll need to make in the very first command. And of course the origin and location of your unnamed 'text file' is unspecified. I created my own file with an arbitrary 101 lines, starting with the 5 numbers you gave as examples and then random in the range 1 to 10000. Note that although my file is in random order, there's no need for that to be so. Choosing a line at random gives you all the randomness you need. (Apart from any pysychological effect you might be seeking!) So I could have sorted it into order, which would perhaps make examining its contents easier.

 

Script:

 

Activate or Launch: Window "Latest - TextPad - [C:\Docs\SUNDRY\Macro Express\Jax-File.txt]", Program "Jax-File.txt", Parameters "" // Now the 'outer loop' will repeat everything 10 times. (You said you wanted to "...pull repeatedly..." but I assume you don't want to so forever!

// I've opened the text file with my text editor, Textpad. You would change that to whatever program YOU have associated with .TXT files.

// You said you want to "...pull repeatedly...", but presumably not forever! So this 'outer loop' does so 10 times.

Repeat Start (Repeat 10 times) // An arbitrary total number of repetitions.

Text Type (Simulate Keystrokes): <CONTROL><HOME> // Ensure text cursor is at start of first line.

Variable Set Integer %nRandomLine% to a random value between 0 and 100 // Choose a random number between 1 and 101.

Repeat Start (Repeat %nRandomLine% times)

Text Type (Simulate Keystrokes): <ARROW DOWN> // Go to the next line.

End Repeat

// The cursor is now on the randomly chosen line.

Text Type (Simulate Keystrokes): <SHIFT><END> // Select the line

Clipboard Copy

Variable Set String %tChosenString% from the clipboard contents

// This now needs to be appended to some other 'text string', whose origin and location is unclear.

// I'll assume the string is already defined in a string variable called tPrefix. That could come from another macro, or part of this macro, or from prompted user input, or whatever.

// I'll define it for this example as 'SomePrefix'.

Variable Set String %tPrefix% to "SomePrefix"

Variable Modify String %tPrefix%: Append Text (%tChosenString%)

// It's not clear what's needed next, so I've elected to simply display the result.

Text Box Display: Result of one random choice

End Repeat // End of the outer loop.

 

Code:

 

<ACTIVATE OR LAUNCH Title="Latest - TextPad - [C:\\Docs\\SUNDRY\\Macro Express\\Jax-File.txt]" Exact_Match="TRUE" Wildcards="FALSE" Path="C:\\Docs\\SUNDRY\\Macro Express\\Jax-File.txt" Mode="\x00" Default_Path="TRUE" Wait="1" Wait_For_Program="12" _COMMENT="Now the 'outer loop' will repeat everything 10 times. (You said you wanted to \"...pull repeatedly...\" but I assume you don't want to so forever!"/>

<COMMENT Value="I've opened the text file with my text editor, Textpad. You would change that to whatever program YOU have associated with .TXT files."/>

<COMMENT Value="You said you want to \"...pull repeatedly...\", but presumably not forever! So this 'outer loop' does so 10 times."/>

<REPEAT START Start="1" Step="1" Count="10" Save="FALSE" _COMMENT="An arbitrary total number of repetitions."/>

<TEXT TYPE Action="0" Text="<CONTROL><HOME>" _COMMENT="Ensure text cursor is at start of first line."/>

<VARIABLE SET INTEGER Option="\x05" Destination="%nRandomLine%" Minimum="0" Maximum="100" _COMMENT="Choose a random number between 1 and 101."/>

<REPEAT START Start="0" Step="1" Count="%nRandomLine%" Save="FALSE"/>

<TEXT TYPE Action="0" Text="<ARROW DOWN>" _COMMENT="Go to the next line."/>

<END REPEAT/>

<COMMENT Value="The cursor is now on the randomly chosen line."/>

<TEXT TYPE Action="0" Text="<SHIFT><END>" _COMMENT="Select the line"/>

<CLIPBOARD COPY/>

<VARIABLE SET STRING Option="\x02" Destination="%tChosenString%"/>

<COMMENT Value="This now needs to be appended to some other 'text string', whose origin and location is unclear."/>

<COMMENT Value="I'll assume the string is already defined in a string variable called tPrefix. That could come from another macro, or part of this macro, or from prompted user input, or whatever."/>

<COMMENT Value="I'll define it for this example as 'SomePrefix'."/>

<VARIABLE SET STRING Option="\x00" Destination="%tPrefix%" Value="SomePrefix"/>

<VARIABLE MODIFY STRING Option="\x06" Destination="%tPrefix%" Value="%tChosenString%"/>

<COMMENT Value="It's not clear what's needed next, so I've elected to simply display the result."/>

<TEXT BOX DISPLAY Title="Result of one random choice" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 Result = \\f1 %tPrefix%\r\n\\par \r\n\\par \\f0 Close this to make another choice. (A total of 10 will be processed in this arbitrary example.)\\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

<END REPEAT _COMMENT="End of the outer loop."/>

 

Macro file:

 

--

Terry, East Grinstead, UK

JaxThread-30Sep2010.mex

Link to comment
Share on other sites

Terry,

Thank you SO much for your time and effort here in providing such valuable assistance. It may be a little 'crude'; nevertheless, it is quite helpful to stimulate the thought process and logic. With the somewhat limited resources available for education and training in MEP, this forum appears to be a real asset.

 

Again, your help is most appreciated!

 

Doug

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...