Jump to content
Macro Express Forums

How do you populate an array


Recommended Posts

Hi, am just returning to Macex after a long absence. Many new features!

 

I'm happy to see the addition of arrays, but I can't seem to get them to work.

 

I have a list of IMDB Top 250 films in a text file (attached). One movie per line.

 

How do I get Macex Pro to parse the file and stick each movie into a separate element?

 

e.g.

movie[1] = The Shawshank Redemption (1994)

movie[2] = The Godfather: Part II (1974)

movie[3] = Il buono, il brutto, il cattivo. (1966)

and so on...

 

I did try the "TEXT FILE BEGIN" command, but all I get is a single variable (movie[1]) filled with the last movie on the list.

 

Regards,

Lemming

IMDB Top 250.txt

Link to comment
Share on other sites

Hi, am just returning to Macex after a long absence. Many new features!

 

I'm happy to see the addition of arrays, but I can't seem to get them to work.

 

I have a list of IMDB Top 250 films in a text file (attached). One movie per line.

 

How do I get Macex Pro to parse the file and stick each movie into a separate element?

 

e.g.

movie[1] = The Shawshank Redemption (1994)

movie[2] = The Godfather: Part II (1974)

movie[3] = Il buono, il brutto, il cattivo. (1966)

and so on...

 

I did try the "TEXT FILE BEGIN" command, but all I get is a single variable (movie[1]) filled with the last movie on the list.

Did you look at the example under the command "Text File Process"? I think its text makes things fairly clear.

Hint: You need to process each line of the file using the variable you declared in the Text File Begin Process - place this code before the Text File End Process command.

Link to comment
Share on other sites

I would use the Split command. You would need to create variables for the end of line characters but that's easy.

Variable Set to ASCII Char 10 to %LF%
Variable Set to ASCII Char 13 to %CR%
Variable Set String set %Test% to the contents of c:\movielist.txt
Split String "%Test%" on "%CR%%LF%" into %Movies%, starting at 1

Use the one below to copy and paste into the editor.

<VARIABLE SET TO ASCII CHAR Value="10" Destination="%LF%"/>
<VARIABLE SET TO ASCII CHAR Value="13" Destination="%CR%"/>
<VARIABLE SET STRING Option="\x03" Destination="%Test%" Filename="c:\\movielist.txt" Strip="FALSE"/>
<SPLIT STRING Source="%Test%" SplitChar="%CR%%LF%" Dest="%Movies%" Index="1"/>

Link to comment
Share on other sites

Hi Cory, thanks for the tip. It works great. I got the array populated as needed.

 

Paul: I might try "Text File Process" again for another project. For now, I will use the Split command. Thanks.

 

-Lemming

 

I would use the Split command. You would need to create variables for the end of line characters but that's easy.

Variable Set to ASCII Char 10 to %LF%
Variable Set to ASCII Char 13 to %CR%
Variable Set String set %Test% to the contents of c:\movielist.txt
Split String "%Test%" on "%CR%%LF%" into %Movies%, starting at 1

Use the one below to copy and paste into the editor.

<VARIABLE SET TO ASCII CHAR Value="10" Destination="%LF%"/>
<VARIABLE SET TO ASCII CHAR Value="13" Destination="%CR%"/>
<VARIABLE SET STRING Option="\x03" Destination="%Test%" Filename="c:\\movielist.txt" Strip="FALSE"/>
<SPLIT STRING Source="%Test%" SplitChar="%CR%%LF%" Dest="%Movies%" Index="1"/>

Link to comment
Share on other sites

Make sure you have the data file in the right place, and its filename is correct.

 

 

Hello all!

 

I am trying to learn this, but I am not getting the same results. The variable %Movies% is nowhere to be found in the debugging window "Show Variable Values". It's not there. I tried a dialog box, but nothing shows there either.

 

Pat

Link to comment
Share on other sites

It's saying Line 4:Debug error: Undefined variable or the variable is the wrong type. This leads me to believe that %Movies% is not defined. Why am I the only one getting this error? How do you define an array variable?
In any examples like these it's assumed any user will properly define the variables referenced. In this case you should define Movies and when you do so check the box "Create as an array" and set the number of elements to a value larger than you need. Hit F1 for help when you have the Variable Properties box open for the help topic which explains it in more detail.
Link to comment
Share on other sites

It's not a waste at all.

 

There are a million and one uses. Let's expand on Lemming's example. Lemming has a nice array that's filled with 250 elements in the form "Movie Title (Year)". Now perhaps I would like to make a list of those I do not have in my movie collection so I can download them. Assume I have all my movies in c:\Movies\. I could repeat 250 times storing the counter in %Counter%. Now each time would have MEP check to see if it's there. If Not File Exists c:\Movies\%movie[%Counter%]%.avi append %movie[%Counter%]% to a text file c:\Movies\2Download.txt. Of course this would only work if you were impeccable in your naming of files.

 

You can also create multiple parallel arrays to in a sense create a table of data where the arrays act like columns in an Excel spreadsheet. ASCII File Process acts a lot the same way but with this you don't need a file and if you use the array over and over you only have to load it once. One macro I just wrote uses arrays. It takes an aging report from an accounting software and a client table from a database. It then goes thru each client in the Db, looks thru the aging report to see if there's a match, if there is do some math to equate a aging value to an account status and check if the status in the Db is correct. If not it adds it to a report for the user to update.

 

That's a simplified example however. But when I have something like a table of data in Excel or such like I suck them into arrays so I can process them there. I even keep the results in an array and then join that array in a way that it can be saved to a TSV file or even pasted into Excel in one go.

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