Jump to content
Macro Express Forums

Can MEX detect Line breaks in Text Variables?


Recommended Posts

For i macro i would like to create, it is necessary to split a larger text variable in indivdual ones. The text should be seperated by line breaks.

 

Say you copied those 3 lines with the clipboard and created a string variable out of it. (%T%)

 

"This is

an awesome

sample text"

 

How do you create those 3 variables:

 

%A%: "This is"

%B%: "an awesome"

%C%: "sample text"

 

Thanks a lot

Alexandra

Link to comment
Share on other sites

There are a couple of ways. First create one or two variables for the carriage return and line feed characters. They are ASCII 13 and 10 respectively. I usually use %CR% and %LF%.

 

Method one is to copy the var to a temporary var and find the position of the LF and copy that chunk to another var and trim. Then delete to the same offset. Then repeat however many times necessary. But the more elegant way is to use the Split String command. Simply split using %CR%%LF% and push it into an array.

Link to comment
Share on other sites

Thank you. Works quite good.

 

I used the Split String command, which i did not think of at first. But i still have a tiny problem. Say i transformed:

 

"This is

an awesome

sample text"

 

via Split String command to the %T% array:

 

%T[1]%: "This is"

%T[2]%: "an awesome"

%T[3]%: "sample text"

 

How can i define a variable which is simply the number of pieces (T-variables) created which is 3 in this example. I need that variable to work as an counter for a Repeat loop.

 

Thank you very much.

Alexandra

Link to comment
Share on other sites

Here's some sample code that does what you want. After the split has been done, it loops backwards through the text array, e.g. T, to find the first %T[n]% variable with a length > 0. Why backwards? If your string were

"This is"

""

"an awesome"

"sample text"

then looping forward would stop after the first line.

Variable Set to ASCII Char 13 to %tCr%
Variable Set to ASCII Char 10 to %tLf%
Variable Set String %tCrLf% to "%tCr%%tLf%"
Variable Set String %tString% to "This is
an awesome
sample text"
Split String "%tString%" on "%tCrLf%" into %T%, starting at 1
Repeat Start (Repeat 99 times)
 Variable Set Integer %iLen% to the length of variable %t[%iCounter%]%
 If Variable %iLen% Is Greater Than "0"
Repeat Exit
 End If
End Repeat
Text Box Display:

<VARIABLE SET TO ASCII CHAR Value="13" Destination="%tCr%"/>
<VARIABLE SET TO ASCII CHAR Value="10" Destination="%tLf%"/>
<VARIABLE SET STRING Option="\x00" Destination="%tCrLf%" Value="%tCr%%tLf%"/>
<VARIABLE SET STRING Option="\x00" Destination="%tString%" Value="This is\r\n\r\nan awesome\r\nsample text"/>
<SPLIT STRING Source="%tString%" SplitChar="%tCrLf%" Dest="%T%" Index="1"/>
<REPEAT START Start="99" Step="-1" Count="99" Save="TRUE" Variable="%iCounter%"/>
<VARIABLE SET INTEGER Option="\x0D" Destination="%iLen%" Text_Variable="%t[%iCounter%]%"/>
<IF VARIABLE Variable="%iLen%" Condition="\x03" Value="0" IgnoreCase="FALSE"/>
<REPEAT EXIT/>
<END IF/>
<END REPEAT/>
<TEXT BOX DISPLAY Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\lang3081\\f0\\fs16 Number of variables used is %iCounter%\\lang1033\\f1 \r\n\\par }\r\n" Left="Center" Top="Center" Width="278" Height="200" Monitor="1" OnTop="FALSE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

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