Remove Spaces at end of variable

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Remove Spaces at end of variable

Post by Snickers » Tue Jun 16, 2015 7:33 pm

Summary question:
How can I detect and remove an empty space or empty spaces at the end of a variable?

Explanation:
An email containing the following phrases are received and my script removes the parentheses and everything in it.

1000 3.5x2 Premium Business Cards ($39)
12in x 24in (+$1/per)
Free soft carry case (+$0)

these items all have the same number of spaces between the end of the product/product desc and the opening parentheses.

I've tried a number of things. I've added an arbitrary string, "pt8", to the end and then I do a position for %emptyspace%pt8
if it finds a positionPos>1 then I reduce the length by 1 and midstr it.

test script produces the following variables:
1000 3.5x2 Premium Business Cards
12in x 24in
Free soft carry c

I have no idea why it is killing the "ase" off of case.

the following code is the latest code I've tried because the simple stuff doesn't seem to work.

//read the line
ReadLn>%fileName%,%r%,productitemline2
//get length of that line
Length>%productitemline2%,productitemline2len
//get text from line omitting first 5 empty spaces
MidStr>%productitemline2%,6,%productitemline2len%,productitemext
//find parentesis in the line ( ( ( ( ( ( ( ( ( ( ( (
Position>%paren%,%productitemext%,1,productitemparentheses
//if parenthesis exists save order item
if>%productitemparentheses%>1
//set length of product item to omit parentheses & price
let>productitemparenthesespos=%productitemparentheses%-1
MidStr>%productitemext%,1,%productitemparenthesespos%,productItemAdmission
//remove space from productItemAdmission
let>productItemAdmission=%productItemAdmission%pt8
mdl>%productItemAdmission%
Position>%spacept8%,%productItemAdmission%,1,spacept8Pos
if>%spacept8Pos%>1
let>productitemparenthesespos2=%productitemparenthesespos%-1
MidStr>%productItemAdmission%,1,%productitemparenthesespos2%,productItemAdmission
mdl>1st trigger: length:%CRLF%%productitemparenthesespos2%%CRLF%%productitemext%%CRLF%%productItemAdmission%
else
MidStr>%productitemext%,1,%productitemparenthesespos%,productItemAdmission
mdl>2nd trigger: length:%CRLF%%productitemparenthesespos%%CRLF%%productitemext%%CRLF%%productItemAdmission%
endif

Dominic_Fichera
Pro Scripter
Posts: 82
Joined: Mon Mar 24, 2014 12:15 pm

Re: Remove Spaces at end of variable

Post by Dominic_Fichera » Tue Jun 16, 2015 9:37 pm

Hi Snickers,

If you're trying to remove trailing characters, have you considered trimming them off? https://www.mjtnet.com/manual/trim.htm. Shouldn't impact your variable of there isn't a trailing character :)

Dominic

Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Re: Remove Spaces at end of variable

Post by Snickers » Tue Jun 16, 2015 10:15 pm

That is the exact function I am looking for.
Thank you!

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Remove Spaces at end of variable

Post by hagchr » Wed Jun 17, 2015 12:47 pm

You could also use Separate> and Trim> to extract the order item:

Code: Select all

Let>x1=      1000 3.5x2 Premium Business Cards  ($39)

Separate>x1,(,res
Trim>res_1,result
MDL>result
(If there could be parentheses also in the order item then you would have to adjust the result eg through concatenating all items until the last left parenthesis. res_1 gives item 1, res_count gives the total number of separated items.)

Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

Re: Remove Spaces at end of variable

Post by Snickers » Wed Jun 17, 2015 7:57 pm

Dominic_Fichera,
Thank you for pointing out Trim. Seriously cleaned my code up!

hagchr,
using separate along with trim shortened my entire code by over 100 lines of code...taking into account each subroutine that included a search for parentheses and extra spaces.

I love how separate doesn't throw an error if the variable doesn't actually have a parentheses in it.


Big thanks guys!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts