Jump to content
Macro Express Forums

count cr/lf from clipboard


Recommended Posts

I just need a simple formula that will output the # of text lines from the clipboard.

 

e.g.

 

the quick

brown fox

jumps over

the laxy

dog

 

if we are to start the clipboard from "the" and ends with "dog" then the output should be 5 lines.

 

any ideas?

Link to comment
Share on other sites

- Save the contents of the clipboard to a variable, e.g. %tClip%

- Save Ascii 13 to a variable, e.g. %tCr%

- Save Ascii 10 to a variable, e.g. %tLf%

- Save the length of %tClip% into a variable, e.g. %nLen%

- Replace all %tCr%%tLf% with "" (i.e. a zero-length string) in %tClip%

- Save the new length of %tClip% into a variable, e.g. %nLenNew%

- Subtract %nLenNew% from %nLen% into a variable, e.g. %nDiff%

- Divide %nDiff% by 2 - and this is your answer

 

Beware of any final line in the clipboard - if it doesn't end with %tCr%%tLf%, then you'll need to allow for this (e.g. extract the last 2 characters of %tClip% and compare with %tCr%%tLf% - if unequal, append %tCr%%tLf% to %tClip%).

Link to comment
Share on other sites

- Save the contents of the clipboard to a variable, e.g. %tClip%

- Save Ascii 13 to a variable, e.g. %tCr%

- Save Ascii 10 to a variable, e.g. %tLf%

- Save the length of %tClip% into a variable, e.g. %nLen%

- Replace all %tCr%%tLf% with "" (i.e. a zero-length string) in %tClip%

- Save the new length of %tClip% into a variable, e.g. %nLenNew%

- Subtract %nLenNew% from %nLen% into a variable, e.g. %nDiff%

- Divide %nDiff% by 2 - and this is your answer

 

Beware of any final line in the clipboard - if it doesn't end with %tCr%%tLf%, then you'll need to allow for this (e.g. extract the last 2 characters of %tClip% and compare with %tCr%%tLf% - if unequal, append %tCr%%tLf% to %tClip%).

 

how do you save the length of a %tClip% into a variable?

Link to comment
Share on other sites

This question doesn't make sense! %tClip% is the name of a text variable, into which I have copied the contents of the clipboard (see 1st line of my suggestions).

 

when you say %tClip% you are referring to the text variable %T[XX]% right? what I dont understand is the saving lenght part. As I understand it, it counts the character of whatever is stored in the text variable.

 

would it be ok if i request an actual code? so that i can see how it is done.

 

thanks in advance paul :)

Link to comment
Share on other sites

when you say %tClip% you are referring to the text variable %T[XX]% right? what I dont understand is the saving lenght part. As I understand it, it counts the character of whatever is stored in the text variable.

 

would it be ok if i request an actual code? so that i can see how it is done.

 

thanks in advance paul :)

I have chosen to call my variable %tClip%. You can call it %T[1]% if you like, the difference being that your variable forms the 1st element of an array.

Let me describe the process to you like this:

On my clipboard is this text:

 

The quick

brown fox

jumps over the

lazy dog

 

This text consists of 4 lines, each of which ends with 2 invisible characters (carriage return = ASCII 13, line feed = ASCII 10)

So this text is exactly 48 characters long (35 non-space characters, 5 spaces, 4 carriage returns, 4 line feeds).

If you store this text in a variable, its length will be 48 characters.

If you now change all carriage return/line feed combinations to nothing, the length of the variable will become 40 (35 non-space characters, 5 spaces, 0 carriage returns, 0 line feeds).

If you now subtract the new length of 40 from the original length of 48, you get 8.

If you now divide 8 by 2 (2, because we removed 2 characters for each line), you get 4 - which is the answer to your original question "how many lines of text do I have?".

Link to comment
Share on other sites

I have chosen to call my variable %tClip%. You can call it %T[1]% if you like, the difference being that your variable forms the 1st element of an array.

Let me describe the process to you like this:

On my clipboard is this text:

 

The quick

brown fox

jumps over the

lazy dog

 

This text consists of 4 lines, each of which ends with 2 invisible characters (carriage return = ASCII 13, line feed = ASCII 10)

So this text is exactly 48 characters long (35 non-space characters, 5 spaces, 4 carriage returns, 4 line feeds).

If you store this text in a variable, its length will be 48 characters.

If you now change all carriage return/line feed combinations to nothing, the length of the variable will become 40 (35 non-space characters, 5 spaces, 0 carriage returns, 0 line feeds).

If you now subtract the new length of 40 from the original length of 48, you get 8.

If you now divide 8 by 2 (2, because we removed 2 characters for each line), you get 4 - which is the answer to your original question "how many lines of text do I have?".

 

 

i see what you mean. but what if the text variable is dynamic? this is where i am puzzled. it will compute how many lines that i have copied from clipboard to a text variable...

 

thanks paul for the explanation :)

Link to comment
Share on other sites

but what if the text variable is dynamic? this is where i am puzzled. it will compute how many lines that i have copied from clipboard to a text variable...

Variables by their nature are dynamic, otherwise we’d use constants;)

 

Paul’s routine is the same as I often use though you cause just the CR or the LF, doesn’t need to be both. Paul’s routine will give you the number of instances regardless of how many ‘lines’ there are.

Link to comment
Share on other sites

Variables by their nature are dynamic, otherwise we’d use constants;)

 

Paul’s routine is the same as I often use though you cause just the CR or the LF, doesn’t need to be both. Paul’s routine will give you the number of instances regardless of how many ‘lines’ there are.

 

but how do I go on counting the # of characters from a text variable?

 

when i say dynamic, it refers to copy`ing text from anywhere and putting it into a text variable. sorry if i confuse you guys :)

Link to comment
Share on other sites

but how do I go on counting the # of characters from a text variable?

 

when i say dynamic, it refers to copy`ing text from anywhere and putting it into a text variable. sorry if i confuse you guys :)

Do you know how to write the command to obtain the length of a variable? If not, then I don't think I know how to help you.

 

Populating a text variable and obtaining its length gives you the number of characters contained by the text variable.

%tVar% = "this is a rabbit"
Length of %tvar% is 16

therefore %tVar% contains 16 characters.

Link to comment
Share on other sites

but how do I go on counting the # of characters from a text variable?

Variable Set to ASCII Char 13 to %CR% // Create a var for the Carrige Return character
Text Box Display: Stop
Variable Set String %Text% from the clipboard contents // Get some text
Variable Set String %Text Count% to "%Text%" // Make a copy to preserve the original
Variable Set Integer %Before% to the length of variable %Text Count% // Count the total number of characters before sub
Variable Modify String: Replace "%CR%" in %Text Count% with "" // Replace all instances of CR with nothing
Variable Set Integer %After% to the length of variable %Text Count% // Count the total number of characters after sub
Variable Modify Integer: %Result% = %Before% - %After% // Subtract to find the number of instances
Text Box Display: %Result%

See attached example.

Test Count.mex

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