1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

conditionally drop trailing \

Discussion in 'T&T - Functions' started by cbrenner, Mar 12, 2013.

  1. cbrenner

    Joined:
    Dec 3, 2008
    Messages:
    2
    Likes Received:
    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?
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,784
    Likes Received:
    29
    You can get rid of a trailing backslash like this.
    Code:
    v:\> echo %@left[-1,ab\]
    ab
     
  3. Howard Goldstein

    Joined:
    Jun 1, 2008
    Messages:
    111
    Likes Received:
    1
    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
     
  4. cbrenner

    Joined:
    Dec 3, 2008
    Messages:
    2
    Likes Received:
    0
    Thanks. These ways are much better.
     

Share This Page