Jump to content
Macro Express Forums

Get position of special characters


garlicnonions

Recommended Posts

I am trying to locate the cr and lf (carriage return and line feed) characters using "get position of text" option in set integer variables. The only way I can think of doing it is to assign the ascii character 10 and 13 to a text variable and then entering that into the function above as the text to search for.  I only get 0 as the position which I believe means its not found.  Is there a way to do this?

 

Thanks, John

Link to comment
Share on other sites

// Line Feed (New Line) character ascii 10
Variable Set to ASCII Char 10 to %LINEFEED%
// Carriage Return character ascii 13
Variable Set to ASCII Char 13 to %CARRIAGERETURN%
// Carriage Return / Line Feed combination characters ascii 13 + ascii 10
Variable Set to ASCII Char 13 to %CRLF%
Variable Modify String %CRLF%: Append Text String Variable (%LINEFEED%)
//
Variable Set Integer %position% to the position of "%CRLF%" in %text%

 

It sounds like that is what you are doing.  You don't, by chance, have the 13 and the 10 reversed?

 

Link to comment
Share on other sites

  • Thank you for your reply. I think the problem might be that this wont work with control characters in ascii such as CR and LF? I tried using just the ASCII 13 character and it doesn't find a single instance of it in a text file full of them.  Also I am working in ME3, don't know if that could also be the issue.

Thank you. John

Link to comment
Share on other sites

ME3 should be fine.  I used ME3 until recently.  No problem finding LF, CR, and CRLF in Notepad and MS Word. 

 

If your text is stored in Unicode you will have problems with ME.  I'm a little fuzzy in my mind as to just what Unicode is, and which applications use it.  In the past I have had success opening these files with Notepad, then saving Notepad as ANSI, then having the macro work with the ANSI file. 

Link to comment
Share on other sites

So tried a real simple test to see if "get position of text" would find CR. I specifically saved a notepad file as ANSI and verified it was ansi with notepad ++. Still unable to find CR. The variable assigned the position returns 0 for all CR characters in the text.  Just to be sure I tried 113 instead of 13 to see if this would locate the letter q in my text and it did.  

image.png.015460b5ca5558d02bd9c3b8dd1c8815.png

Link to comment
Share on other sites

The Text File Begin Process will read one line at a time and put it into your variable %T1% ... without a CRLF. Variable %T1% will not contain a CRLF.

 

In your example the first time through the Text File Begin Process loop %T1% will contain "My Requisition Statuses"  (no CRLF).

The second time through the loop %T1% will contain "Create Requisition" (again, no CRLF).

The third time through the loop %T1% will contain "Procurement Templates" (still no CRLF).

 

To see how these things work load the file asniSavedCopy.txt into variable %T2%. Then do the "Variable Set Integer %N1% from Position of Text in variable %T2%. The variable %N1% will be set to 24 (Assuming the first line contains "My Requisition Statuses<CR><LF>"

 

What are you trying to accomplish? We might be able to help you better if we understand your goal.

Link to comment
Share on other sites

"In your example the first time through the Text File Begin Process loop %T1% will contain "My Requisition Statuses"  (no CRLF)."

 

...I would have thought the CRLF would be included at the end of the line of text saved to %T1%, just as it shows in the screenshot...why is it not?

 

I tried to do as you suggested, but no success. I am not sure i did it correctly though. What i tried was this:

image.png.db926c51ab074ffe30e1f33e521d57bd.png

 

where the contents of the "variable set integer" command are :

image.png.ed2f000f1032f8203bd2e938a8342bed.png

 

....

 

 

As for what Im trying to do:

I am copying a webpage, locating the  section containing a list of items, then generating a csv file from that list.  The text copied from the webpage looks as follows, with the three repeating sections representing 3 separate items, each has 8 lines of text.  Each item will be one row in a csv file.  The reason I am trying to locate the CRLF is to find the blank section below the 3rd item which identifies the end of my area of interest. So I was thinking if i could locate repeating CRLF characters that followed the last item consecutively repeating ...as in the blank area I would define the end of my area of interest.  I have a work around of counting blank lines , as in "" assigned to a text variable. I would still like to be able to locate CRLF as I think it would simplify my code and would be useful in future projects.

image.png.ce29ab7552eb27ffdc3ef728d6f61267.png

Link to comment
Share on other sites

Text File Begin Process reads one line at a time, and I believe it strips the CRLF, so your macro will never see the CRLF (as you have found!)

 

I think Samrae is suggesting that you use the command Variable Set String, with option "Set Value from File."  That single command loads the ENTIRE file into a text variable, bang, all at once.  See illustration below -- my screen shot is ME Pro, but ME3 is similar.  When you use this command, the data loaded into the variable retains the CRLF characters.  The checkbox to Strip Trailing CR/LF is deceptive -- if you check the box, it strips the FINAL CRLF from the end of the file, but does NOT strip any others.  So you could get the entire file into one variable, then write macro logic to parse out the individual lines, with CRLF sequences where you expect to see them. 

 

setstring.JPG

  • Thanks 1
Link to comment
Share on other sites

17 minutes ago, garlicnonions said:

I cant use the "text file process" function to loop through the text on line at a time. I will need to create my own loop using the "Variable Set String" command?

Right, you can not use "text file process" because ME brings in one line at a time into a variable, but does NOT bring in the CRLF.

 

"Variable Set String" will not be part of your loop.  It will be executed only once, to bring the entire text file into a variable.  CRLF sequences will just be more characters in a single long undifferentiated string in the variable.  Then a loop can extract each line, one at a time, by locating the next CRLF. 

 

The variable containing the whole text file will look something like this: "Line1textCRLFLine2textCRLFLine3textCRLFLine4textCRLF.... "  It is only in your mind, or in Notepad, that the string is organized into separate lines.

Link to comment
Share on other sites

You can still use Text File Process to loop through the text.

 

Each line of a text file used by Windows ends with CR followed by LF referred to as CRLF. The Text File Begin/End process reads a line of text up to the CRLF. For convenience in your macro the string read by Text File Process does not include the CRLF.  However, in your case, you know that each line originally had the CRLF. So, to determine the number of CRLF's just count the lines. Something like this:

 

Variable Set Integer %N99% to 0
Text File Begin Process: "ansisavedcopy.txt"
  Variable Modify Integer: Inc (%N99%)
  
  // If the line is empty then stop the Text File loop
  If Variable %T1% = ""
    Break
  End If
Text File End Process

// Here: %N99% contains the number of times through the loop. This is also the number of CRLF's found.

 

This sample does what you described. I counts the number of CRLF's found and it stops when the line is blank.

 

There are some file formats that use only CR or only LF. If that were the case then you would have to read the entire file into a variable and parse through it. But, since Notepad+ indicates the file is ANSI format we know that each line of your file ends with CRLF.

 

  • Thanks 1
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...