conditionally drop trailing \

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Dec 3, 2008
2
0
#1
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?
 
#3
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