TCL: List command -lsearch, LSORT, LRANGETCL command lsort -unique

2023-03-18  



command format:

lsearch list pattern

lsort list

lrange list first last

Example:

set list [list {Washington 1789} {Adams 1797} {Jefferson 1801} \

               {Madison 1809} {Monroe 1817} {Adams 1825} ]

set x [lsearch $list Washington*]

set y [lsearch $list Madison*]

incr x

incr y -1                        ;# Set range to be not-inclusive

set subsetlist [lrange $list $x $y]

puts “The following presidents served between Washington and Madison”

foreach item $subsetlist {

    puts “Starting in [lindex $item 1]: President [lindex $item 0] “

}

set x [lsearch $list Madison*]

set srtlist [lsort $list]

set y [lsearch $srtlist Madison*]

puts “\n$x Presidents came before Madison chronologically”

puts “$y Presidents came before Madison alphabetically”

execute results:

The following presidents served between Washington and Madison

Starting in 1797: President Adams

Starting in 1801: President Jefferson

3 Presidents came before Madison chronologically

3 Presidents came before Madison alphabetically

Attach a common list command:

1          listcommand

            grammar: list ? value value…?

            This command generates one

The element oflistlistis allvalue. example:

            % list 1 2 {3 4}

            1 2 {3 4}

concatcommand:

            grammar:concat list ?list…?

            This command takes multiplelistSynthetic onelist, everylistinto newlistA element.

3          lindexcommand

            grammar:lindex list index

            Returnlistindex(0-based)element. example:

            %   lindex  {1 2 {3 4}} 2

            3 4

4          llengthcommand

            grammar:llength  list

            Return

The number of elements oflist. example

            % llength  {1 2 {3 4}}

            3

5          linsertcommand

            grammar:linsert list index value ?value…?

            Return to a new string, the new string is allvalueparameter value insertionlistindex(0-based)Early element was obtained before. example:

            % linsert {1 2 {3 4}}  1   7 8  {9 10}

            1 7 8 {9 10} 2 {3 4}

6          lreplacecommand:

            grammar:lreplace list first last ?value value …?

            Return a new string, the new string islistfirs (0-based)tto the firstlast (0-based)Element uses allvalueParameters are replaced. if there is notvalueparameters, indicate to be deletedfirstto the firstlastElement. example:

            % lreplace {1 7 8 {9 10} 2 {3 4}}  3 3

            1 7 8 2 {3 4}

            % lreplace {1 7 8 2 {3 4}}  4  4  4 5 6

            1 7 8 2 4 5 6

7          lrange command:

            grammar:lrange list first last

            Returnlistfirst (0-based)to the firstlast (0-based)composition of element,If

The value oflastisend. It isfirstuntil the end of the string.

            Example:

            % lrange  {1 7 8 2 4 5 6} 3 end

            2 4 5 6

8          lappendcommand:

            grammar:lappend varname value ?value…?

            The value ofvalueis added to the variable as an element

Aftervarname, and return the new value of the variable, ifvarnameNo existence, this variable is generated. example:

            % lappend  a  1 2 3

            1 2 3

            % set a

            1 2 3

9          lsearch command:

            grammar:lsearch ?-exact? ?-glob? ?-regexp? list pattern

            Return

The first match mode inlistpatternThe index of the element, if you can’t find a match, return-1-exact-glob -regexpis a technology matching technology.-exactindicate precise matching;-globmatching method and

The matching method ofstring matchcommand is the same, and will be introduced in the eighth section of the latter eighth sectionstringcommand time introduction;-regexprepresents a formal expression matching, and will be introduced in the eighth quarterregexpcommand time introduction. Disposal time use-globmatch. example:

            % set  a  { how are you }

             how are you

            % lsearch $a  y*

            2

            % lsearch $a y?

            -1

10         lsortcommand:

            grammar:lsort ?options? list

            This command returnslistString after sorting.optionscan be the following value:

            -ascii    PressASCIIcharacter order sort comparison.This is a default situation.

            -dictionary    Sort by dictionary,and-asciiDifferent places are:

                          (1)Do not consider writing

                          (2)If there are numbers in the element,Numbers are sorted as an integer.

                           Therefore:bigBoybigbangandbigboy, x10y x9yandx11y.

            -integer   The element oflistis converted into an integer,Sort by integer.

            -real       listThe element of 101 is converted into a floating point number,Sort by floating point number.

            -increasing    lift order(PressASCIIcharacter comparison)

            -decreasing    Substitute(PressASCIIcharacter comparison)

            -command   command TCLautomatic use

Thecommand command compares each two elements one by one,and then give the sort results.

11         splitcommand:

            grammar:split string ?splitChars?

            Parce stringstringPress the separatorsplitCharsis divided into words, returning a string composed of these words. ifsplitChars

            is an empty character{}stringis separated by characters. ifsplitCharsNo give up,Twither spaces as separators. example:

            % split “how.are.you” .

            how are you

            % split “how are you”

            how are you

            % split “how are you” {}

            h o w { } a r e { } y o u

12         joincommand

            grammar

The:join list ?joinString? 

            joincommand is the reverse of the command. This command handle

All elements oflistmerge into a string, in the middlejoinStringSeparate. DefaultjoinStringis a space. example:

            % join {h o w { } a r e { } y o u} {}

            how are you

            % join  {how are you} .

            how.are.you

source

Random Posts

[OpenCV] Mindvision camera GetImage

Unity’s handles drawing round, point-six

Vue projects in the project Vue top effect

Center7 virtual machine boot network card does not start solution to BLANK

HDU2014 Young Singer Grand Prix Jury to score IT