Skip to content

Commit

Permalink
Added list utilities (diff, intersect) in alltools
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycatdevs committed Jan 31, 2024
1 parent d60cb1a commit 23affed
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions scripts/alltools.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# Tothwolf 27Dec2003: added matchbotattrany, optimized ordnumber,
# more minor changes
# CrazyCat 18Oct2022: added achannels
# CrazyCat 31Jan2024: added lintersect and ldiff
#
########################################
#
Expand Down Expand Up @@ -127,6 +128,11 @@
# if the given number is between 1 and 15, return its text representation
# else return the number given
#
# lintersect <list1> <list2>
# returns a list of all elements in list1 that are present in list2
#
# ldiff <list1> <list2>
# returns a list of all elements in list1 which are not in list2
#
##
## (other commands):
Expand Down Expand Up @@ -384,6 +390,16 @@ proc number_to_number {number} {
}
}

proc lintersect {list1 list2 {option -exact}} {
if {$option ne "-nocase"} { set option -exact }
return [lmap x $list1 {expr {[lsearch $option $list2 $x] >= 0 ? $x : [continue]}}]
}

proc ldiff {list1 list2 {option -exact}} {
if {$option ne "-nocase"} { set option -exact }
return [lmap x $list1 {expr {[lsearch $option $list2 $x] < 0 ? $x : [continue]}}]
}

#
# other commands:
#
Expand Down

0 comments on commit 23affed

Please sign in to comment.