TABLE OF CONTENTS


::pwtk::cut

SYNOPSIS

proc ::pwtk::cut {text {N {}} {M {}}} {

USAGE

   cut text ?N? ?M?

EXAMPLES

   cut $text 13
   cut $text 5 end
   cut $text 3 9

PURPOSE

Similar to Unix 'cut' command but takes a text string as the argument, i.e:

cut $text N ... cut each line in the text to make it at most N characters long cut $text N M ... cut each line in the text from N'th to M'th (included) character

                     (the 1st character has index 1)

ARGUMENTS

if $N < 0 || $N == {} || $N == "end", no cutting is performed (i.e. verbatim text is returned).

SOURCE

    if { ! [::pwtk::type::number integer $N] && $N ne "end" } {
        ::pwtk::error "expected integer  or 'end' but got: $N\nwhile executing [procName] $N $M" 1
    }
    if { ! [::pwtk::type::number integer $M] && $M ne "end" } {
        ::pwtk::error "expected integer or 'end' but got: $M\nwhile executing [procName] $N $M" 1
    }
    if { $N < 0 || $N eq {} || $N eq "end" } {
        return $text
    }
    if { $M ne {} } {
        set start $N-1
        if { $M ne "end" } { set end $M-1 } else { set end end }
    } else {
        set start 0
        set end   $N-1
    }

    set result {}
    foreach line [split $text \n] {
        append result [string range $line $start $end]\n
    }
    return $result
}