Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

conditionally drop trailing \

Dec
5
0
I need a function to create a canonical version of a folder path, with leading but not trailing \.
For example, typical input could be \mx\durango\ or mx\durango\ or mx\durango\.
Output: \mx\durango

My solution is so complicated that I think there must be a better way.
First, I wrote companion functions to delete the leading or trailing or leading slashes if any:
Function NoLeadSlash=`%@If[%@Left[1,%1] == \,%@Instr[1,,%1],%1]`
Function NoTrailSlash=`%@If[%@Right[1,%1] == \,
%@Instr[0,%@Eval[%@Len[%1]-1],%1],%1]`
(please ignore the gap or line break after \, )

The first one isn't too bad, but for the second do I really need to use @LEN[]? And further, having used it and subtracting 1, is there something better than @Eval[]?

Given the above, the following to finish the job are not too complex.
Function NoEndSlash=`%@NoLeadSlash[%@NoTrailSlash[%1]]`
Function LeadSlash=`%@If[%1. == .,,\%@NoEndSlash[%1]]`

What am I missing?
 
You can get rid of a trailing backslash like this.
Code:
v:\> echo %@left[-1,ab\]
ab
 
I need a function to create a canonical version of a folder path, with leading but not trailing \.
For example, typical input could be \mx\durango\ or mx\durango\ or mx\durango\.
Output: \mx\durango

My solution is so complicated that I think there must be a better way.
First, I wrote companion functions to delete the leading or trailing or leading slashes if any:
Function NoLeadSlash=`%@If[%@Left[1,%1] == \,%@Instr[1,,%1],%1]`
Function NoTrailSlash=`%@If[%@Right[1,%1] == \,
%@Instr[0,%@Eval[%@Len[%1]-1],%1],%1]`
(please ignore the gap or line break after \, )

The first one isn't too bad, but for the second do I really need to use @LEN[]? And further, having used it and subtracting 1, is there something better than @Eval[]?

Given the above, the following to finish the job are not too complex.
Function NoEndSlash=`%@NoLeadSlash[%@NoTrailSlash[%1]]`
Function LeadSlash=`%@If[%1. == .,,\%@NoEndSlash[%1]]`

What am I missing?
The simplest way I can think of is:

Code:
function noEndSlash=`%@ltrim[\,%@rtrim[\,%1]]`
function LeadSlash=`\%@NoEndSlash[%1]`

Or, if you don't need the intermediate NoEndSlash function, it's simply:
Code:
function LeadSlash=`\%@ltrim[\,%@rtrim[\,%1]]`

--
Howard
 
Back
Top