Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string trimleft question

398 views
Skip to first unread message

Adam Jensen

unread,
Sep 19, 2016, 6:17:23 PM9/19/16
to
% info patchlevel
8.6.5

When I do this:

set foo /usr/home/hanzer/LCARS/Entertainment/Star_Trek/TOS/S01
set blah /usr/home/hanzer/LCARS/Entertainment/Star_Trek
string trimleft $foo $blah

The result is this:
OS/S01

But I expected this:
/TOS/S01

Why did 'string trimleft' cut the "/T" characters?


Lew Pitcher

unread,
Sep 19, 2016, 6:36:46 PM9/19/16
to
On Monday September 19 2016 18:17, in comp.lang.tcl, "Adam Jensen"
Because both were found in the $blah set of characters

According to the TCL documentation page
https://www.tcl.tk/man/tcl8.4/TclCmd/string.htm#M46

string trimleft string ?chars?
Returns a value equal to string except that any leading characters present
in the string given by chars are removed.

The ?chars? parameter is a list of characters that may be removed from the
left hand side of the string, stopping at the first character in the string
that does not match any character in the ?chars? list.

In your example
set foo /usr/home/hanzer/LCARS/Entertainment/Star_Trek/TOS/S01
set blah /usr/home/hanzer/LCARS/Entertainment/Star_Trek
string trimleft $foo $blah
the left hand side of $foo, from the first / to the /T of "/TOS" match
characters in the set given by $blah. So, the trimleft removes them. The
first "O" in $foo does not match any character in $blah which terminates the
trim function. This leaves (from $foo) OS/S01 untrimmed.

--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

Adam Jensen

unread,
Sep 19, 2016, 6:52:42 PM9/19/16
to
On 09/19/2016 06:36 PM, Lew Pitcher wrote:
>
> Because both were found in the $blah set of characters
>
> According to the TCL documentation page
> https://www.tcl.tk/man/tcl8.4/TclCmd/string.htm#M46
>
> string trimleft string ?chars?
> Returns a value equal to string except that any leading characters present
> in the string given by chars are removed.
>
> The ?chars? parameter is a list of characters that may be removed from the
> left hand side of the string, stopping at the first character in the string
> that does not match any character in the ?chars? list.
>
> In your example
> set foo /usr/home/hanzer/LCARS/Entertainment/Star_Trek/TOS/S01
> set blah /usr/home/hanzer/LCARS/Entertainment/Star_Trek
> string trimleft $foo $blah
> the left hand side of $foo, from the first / to the /T of "/TOS" match
> characters in the set given by $blah. So, the trimleft removes them. The
> first "O" in $foo does not match any character in $blah which terminates the
> trim function. This leaves (from $foo) OS/S01 untrimmed.
>

Ah, ?chars? is a SET of characters! Thanks.

Is there a simple way to trim a SEQUENCE of characters (a sub-string)
from a string? e.g., trim $blah from $foo ?


Adam Jensen

unread,
Sep 19, 2016, 7:00:23 PM9/19/16
to
On 09/19/2016 06:52 PM, Adam Jensen wrote:
[snip]
> Is there a simple way to trim a SEQUENCE of characters (a sub-string)
> from a string? e.g., trim $blah from $foo ?

This does it but it seems there would be a more straight forward gizmo
built in.

string range $foo [string length $blah] end

Robert Heller

unread,
Sep 19, 2016, 8:23:22 PM9/19/16
to
It is important to understand that the second argument to string
trim/trimright/trimleft is a *set* in the form of a string. It is not
something that is string matched. It is a *set* of characters. Do not think
of it as a string. It is not a string, it is a set.

Observe:

gollum.deepsoft.com% tclsh
% set foo /usr/home/hanzer/LCARS/Entertainment/Star_Trek/TOS/S01
/usr/home/hanzer/LCARS/Entertainment/Star_Trek/TOS/S01
% set blah "/usrhomeanzLCARSEti_Tk"
/usrhomeanzLCARSEti_Tk
% string trimleft $foo $blah
OS/S01



>
>
>

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

Robert Heller

unread,
Sep 19, 2016, 8:23:27 PM9/19/16
to
At Mon, 19 Sep 2016 18:17:19 -0400 Adam Jensen <han...@riseup.net> wrote:

>
Consider this:

% set foo /usr/home/hanzer/LCARS/Entertainment/Star_Trek/TOS/S01
/usr/home/hanzer/LCARS/Entertainment/Star_Trek/TOS/S01
% set blah /usr/home/hanzer/LCARS/Entertainment/Star_Trek
/usr/home/hanzer/LCARS/Entertainment/Star_Trek
% regsub "^$blah" $foo {}
/TOS/S01

Adam Jensen

unread,
Sep 19, 2016, 9:21:47 PM9/19/16
to
On 09/19/2016 08:23 PM, Robert Heller wrote:
> Consider this:
>
> % set foo /usr/home/hanzer/LCARS/Entertainment/Star_Trek/TOS/S01
> /usr/home/hanzer/LCARS/Entertainment/Star_Trek/TOS/S01
> % set blah /usr/home/hanzer/LCARS/Entertainment/Star_Trek
> /usr/home/hanzer/LCARS/Entertainment/Star_Trek
> % regsub "^$blah" $foo {}
> /TOS/S01

Thanks, that's nifty. I went with this:

string range $foo [string length $blah] end

Now I am trying to cobble together a basic TK GUI...

Rich

unread,
Sep 19, 2016, 10:20:05 PM9/19/16
to
Make that into a proc and then it becomes built in for you.

Alternately, as long as the sequence you want to chop off the front
isn't repeated again within the string, you can abuse 'string map' to
chop it off:

string map [list $blah ""] $foo

Uwe Klein

unread,
Sep 20, 2016, 3:50:04 AM9/20/16
to
regexp ^${blah}(.*)$ $foo -> tail

puts "tail is $tail"

Uwe

Mike Griffiths

unread,
Sep 20, 2016, 4:44:05 PM9/20/16
to
regexp is potentially prone to false-positives here unless you properly escape regexp-special characters in $blah, several of which (most notably ".") are valid in filenames.
0 new messages