Macro Express Forums

# Filling an array from text file

## Recommended Posts

I JUST upgraded to MEP and am trying to fill an array from a text file(ArrayTest.txt). I created a 26 line text file with notepad where each line is a letter of the alphabet (a is first [1], z is last [26]. I'm really having a hard time doing this.

Can someone give me a SIMPLE explanation? I seem to be able to use the TEXT BEGIN PROCESS and END PROCESS to grab the right file but I only get the last line. I think an explanation of setting up the array would be helpful. I'm missing something.

I follow the example and set the TEXT BEGIN PROCESS by giving the right file name and start with record 1 and Variable to receive results set to %T[1]%. Then I put in an TEXT END PROCESS. I run it in Test Mode and look at the variables. All I see is T[1] is z. What am I doing wrong? It looks like T[1] just keeps getting over-written instead of incrementing (T[1],T[2],T[3]...T[26]).

I guess I need some kind of processing in the middle. Maybe copy each T[1] to X[1] then X[2] but how to do that?

##### Share on other sites

I don't have a lot of time to explain in detail but here's how I do it.

1. Create varaibles for CR and LF.EG [Variable Set to ASCII Char 10 to %LF%] and [Variable Set to ASCII Char 13 to %CR%]
2. Read the entire contents of the file into a temp strng var. Say, %temp%
3. Now use the Split String to split %temp% using %CR%%LF% into your array.

##### Share on other sites

Hi chappyware, and welcome to the forum.

Here's one way of doing it. It's deliberately restricted to getting the letters a-z into an array, as I think that's the part that's giving you a problem. So to keep the focus on that I haven't started with a 26-line file as you have (which I assume is because you have a more complex purpose in mind for later).

Variable Set String %String26% to "abcdefghijklmnopqrstuvwxyz"

// Demo for chappyware

Repeat Start (Repeat 26 times) // Start a loop that will repeat 26 times (unless prevented).

Variable Modify Integer %Index%: Increment // Effectively sets a 'normal' integer variable 'Index' to its starting value of 1 and increments it by 1 on every loop.

Variable Modify String: Copy a substring in %String26%, starting at %Index% and 1 characters long to %SingleChar% // Extract a single letter, and call it SingleChar.

Variable Set String %Result[%Index%]% to "%SingleChar%" // The crucial step to build the array. See detailed explanation.

End Repeat

Text Box Display:

Her's the code. Copy it into a new macro to study it. Or import the MEX file I've attached.

<VARIABLE SET STRING Option="\x00" Destination="%String26%" Value="abcdefghijklmnopqrstuvwxyz"/>
<COMMENT Value="Demo for chappyware"/>
<REPEAT START Start="1" Step="1" Count="26" Save="FALSE" _COMMENT="Start a loop that will repeat 26 times (unless prevented)."/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%Index%" _COMMENT="Effectively sets a 'normal' integer variable 'Index' to its starting value of 1 and increments it by 1 on every loop."/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%SingleChar%" Variable="%String26%" Start="%Index%" Count="1" _COMMENT="Extract a single letter, and call it SingleChar."/>
<VARIABLE SET STRING Option="\x00" Destination="%Result[%Index%]%" Value="%SingleChar%" _COMMENT="The crucial step to build the array. See detailed explanation."/>
<END REPEAT/>
<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil Tahoma;}{\\f1\\fnil\\fcharset0 Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs16 Result[1]\\f1  = \\f0 %Result[1]%\r\n\\par Result[\\f1 2\\f0 ]\\f1  = \\f0 %Result[\\f1 2\\f0 ]%\r\n\\par Result[\\f1 3\\f0 ]\\f1  = \\f0 %Result[\\f1 3\\f0 ]%\r\n\\par \\f1 .\r\n\\par .\r\n\\par \\f0 Result[\\f1 26\\f0 ]\\f1  = \\f0 %Result[\\f1 26\\f0 ]%\r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="0" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>


Building the array is a bit tricky because IMO ME Pro doesn't let you do it in the most intuitive way. As you see here, I could not enter '%Index%' directly, but had to temporarily enter a single integer then change it in the next dialog:

--

AlphabetToArray.mex

##### Share on other sites

Thank you. SOOOOOO much

##### Share on other sites

How would I fill %STRING% from a text file instead of defining it at the outset.

##### Share on other sites

Variable Set String set %String% to the contents of c:\test.txt

##### Share on other sites

I'm ALMOST there. My yext file has 26 lines-each line is one letter of the alphabet (a b c d e etc).

This does what I want it to do except I get a 27th element in my array equal to the first ('a').

<VARIABLE SET INTEGER Option="\x00" Destination="%counter%" Value="1"/>

<TEXT FILE BEGIN PROCESS Filename="C:\\alpha.txt" Start_Record="1" Process_All="TRUE" Records="26" Variable="%T[%counter%]%"/>

<VARIABLE MODIFY INTEGER Option="\x07" Destination="%counter%"/>

<VARIABLE SET STRING Option="\x03" Destination="%T[%counter%]%" Filename="c:\\alpha.txt" Strip="FALSE"/>

<TEXT FILE END PROCESS/>

How do I get out after counter = 26? I tried a IF Variable-counter>26 BREAK END IF to no avail.

any suggestions?????

##### Share on other sites

A HA ! I had the IF VARIABLE - BREAK in the wrong spot.

This works perfectly !! Creates T[1]=a to T[26]=z

<VARIABLE SET INTEGER Option="\x00" Destination="%counter%" Value="1"/>

<TEXT FILE BEGIN PROCESS Filename="C:\\alpha.txt" Start_Record="1" Process_All="TRUE" Records="26" Variable="%T[%counter%]%"/>

<VARIABLE MODIFY INTEGER Option="\x07" Destination="%counter%"/>

<IF VARIABLE Variable="%counter%" Condition="\x03" Value="26" IgnoreCase="FALSE"/>

<BREAK/>

<END IF/>

<VARIABLE SET STRING Option="\x03" Destination="%T[%counter%]%" Filename="c:\\alpha.txt" Strip="FALSE"/>

<TEXT FILE END PROCESS/>

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×