Version 35 of scratch

Updated 2024-03-27 11:46:02 by gold

gold Replay old scratch page on TCL Wiki. checking end of line issues here.

This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Aside from your courtesy, your wiki MONIKER and date as a signature and minimal good faith of any internet post are the rules of this TCL-WIKI. Its very hard to reply reasonably without some background of the correspondent on his WIKI bio page. Thanks, gold 26Mar2024


Preface


gold Update 3/26/2024. Here are some calculations for the Letter Scramble Game with Pseudocode outline reference to TCL Procedures. Intended to supplement the TCL Wiki page Playing with Recursion by RS. This supplemental code following RS is intended for study of McCarthy theorems on computer arithmetic into one-line programs. Interesting letter game was Word Chain from Keith Vetter. Useful error test on foreach {input expected} from Playing Cobol from RS etc. Error testing procedures were useful in Simple Test from EKB & RLE & RS. ***find a text in a line and replace the next line***

Note: For those who don't follow comp.lang.tcl, this same individual has been asking the same question there over the course of the last couple weeks. He/She has been extremely resistant to actually learning anything and instead simply keeps asking for the answer to be handed to them on a silver platter. There was one member who continued trying to help but finally gave up in frustration because of the person's unwillingness to learn even the slightest bit of knowledge about "how to program a computer". One conclusion has been that this individual is a beginning student attempting to cheat on his/her homework by getting Usenet (and now the wiki) to do their homework problems for them.

tclamateur: I want to find a text in a line and replace the contents of the next line with a new string. I tried regsub, but it is only matching the first line. I don't know how to go to the next line. eg:

line 5> this is line 5.

line 6> apple

with

line 5> this is line 5.

line 6> banana

Thanks

AMG: [regsub] has the -all option to replace every occurrence, not just the first.

proc replace {match replacement text} {
    # Backslash-quote special characters in match and replacement so they will
    # be interpreted as literal strings.
    regsub -all {[][*+?{}()<>|.^$]} $match {\\&} match
    regsub -all {[\\&]} $replacement {\\&} replacement

    # Perform the replacement.
    regsub -all ($match\\n)\[^\\n\]* $text \\1$replacement
}

replace "this is line 5." banana {
this is line 5.
apple
this is line 5.
another apple
this is not line 5.
yet more apples
}

This gives the following result:

this is line 5.
banana
this is line 5.
banana
this is not line 5.
yet more apples

MG offers:

proc replace2 {args} {

if { [llength $args] < 3} {
     return -code error "wrong # arguments: replace2 ?-all? ?-regexp|-glob|-exact? \$find \$replace \$text"
   }
set find [lindex $args end-2]
set replace [lindex $args end-1]
set text [lindex $args end]
set args [lrange $args 0 end-3
]
set matchtype -exact
set all ""
foreach x $args {
if {$x eq "" } {
    continue;
   } elseif { $x eq "-all" } {
     set all [list "-all"]
   } elseif { $x in [list -regexp -glob -exact] } {
     set matchtype $x
   } else {
     return -code error "unknown option '$x'"
   }
}
set text [split $text \n]
set matches [lsearch {*}$all $matchtype $text $find]
if { [llength $matches] && [lindex $matches 0] != -1 } {
     foreach x $matches {
        incr x
        set text [lreplace $text $x $x $replace]
     }
   }
return [join $text "\n"]

}

# Example:
replace2 -all -exact "this is line 5." banana {
this is line 5.
apple
this is line 5.
another apple
this is not line 5.
yet more apples
}

tclamateur: Thanks for the input. In my case I'll have only two inputs. The input "match" and the input "replacement". What to give as input for "text"?

AMG: Whatever text you want the substitution to be performed on. For instance, if you're working on a file, you'd supply the contents of that file. Then write the return value back out to the file, and you're done.


John K. Ousterhout is founder and CEO of Electric Cloud, Inc. He is also creator of the Tcl scripting language and is well known for his work in distributed operating systems, high-performance file systems, and user interfaces. Here how he describes the events: I got the idea for Tcl while on sabbatical leave at DEC's Western Research Laboratory in the fall of 1987. I started actually implementing it when I got back to Berkeley in the spring of 1988; by summer of that year it was in use in some internal applications of ours, but there was no Tk. The first external releases of Tcl were in 1989, I believe. I started implementing Tk in 1989, and the first release of Tk was in 1991. mORE on the Ousterhout website.


Hidden Comments Section

Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, gold 3/26/2024