Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
string map in Tcl
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Glenn Jackman  
View profile  
 More options Jun 16 2009, 1:48 pm
Newsgroups: comp.lang.tcl
From: Glenn Jackman <gle...@ncf.ca>
Date: 16 Jun 2009 17:48:00 GMT
Subject: string map in Tcl
For my curiosity, is this a complete way to implement [string map] in
plain Tcl?

    proc stringmap {maplist s} {
        if {[llength $maplist] == 0} {
            return $s
        } elseif {[llength $maplist] % 2 != 0} {
            error "char map list unbalanced"
        }
        set new ""
        while {[string length $s] > 0} {
            set replacement [string index $s 0]
            set i 1
            foreach {key value} $maplist {
                set len [string length $key]
                if {$len > 0 && [string match "$key*" $s]} {
                    set replacement $value
                    set i $len
                    break
                }
            }
            append new $replacement
            set s [string range $s $i end]
        }
        return $new
    }

    stringmap {abc 1 ab 2 a 3 1 0} 1abcaababcabababc ;# => 01321221
    stringmap {aa X a Y} baaa ;# => bXY
    stringmap {a Y aa X} baaa ;# => bYYY

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Hobbs  
View profile  
 More options Jun 16 2009, 1:57 pm
Newsgroups: comp.lang.tcl
From: Jeff Hobbs <jeff.ho...@gmail.com>
Date: Tue, 16 Jun 2009 10:57:53 -0700 (PDT)
Local: Tues, Jun 16 2009 1:57 pm
Subject: Re: string map in Tcl
On Jun 16, 10:48 am, Glenn Jackman <gle...@ncf.ca> wrote:

Looks complete, but doesn't support -nocase, which should be possible
since string match has a -nocase option.  You can try this over the
test suite for string map, which should cover all the edge cases.

Jeff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andreas Leitgeb  
View profile  
 More options Jun 17 2009, 8:25 am
Newsgroups: comp.lang.tcl
From: Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>
Date: 17 Jun 2009 12:25:20 GMT
Local: Wed, Jun 17 2009 8:25 am
Subject: Re: string map in Tcl

Jeff Hobbs <jeff.ho...@gmail.com> wrote:
> On Jun 16, 10:48 am, Glenn Jackman <gle...@ncf.ca> wrote:
>> For my curiosity, is this a complete way to implement [string map] in
>> plain Tcl?
>>         set new ""
>>         while {[string length $s] > 0} {
>>             set replacement [string index $s 0]
>>             set i 1
>>             foreach {key value} $maplist {
>>                 set len [string length $key]
>>                 if {$len > 0 && [string match "$key*" $s]} {

To make it really equivalent, you'd have to protect any glob-magic chars
in $key, or rather do something like
  [string equal -length [string length $key] $key $s]
instead of the [string match ...].

> Looks complete, but doesn't support -nocase, which should be possible
> since string match has a -nocase option.

string equal also has -nocase


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Glenn Jackman  
View profile  
 More options Jun 18 2009, 12:53 pm
Newsgroups: comp.lang.tcl
From: Glenn Jackman <gle...@ncf.ca>
Date: 18 Jun 2009 16:53:04 GMT
Local: Thurs, Jun 18 2009 12:53 pm
Subject: Re: string map in Tcl
At 2009-06-17 08:25AM, "Andreas Leitgeb" wrote:

> >> if {$len > 0 && [string match "$key*" $s]} {

>  To make it really equivalent, you'd have to protect any glob-magic chars
>  in $key, or rather do something like
>    [string equal -length [string length $key] $key $s]
>  instead of the [string match ...].

Good tip, thanks.

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »