Finding duplicate entries in a list

tcl

    Next

  • 1. Tk text search and elided text
    All, I'm having trouble understanding the following text widget behavior. I'm using Tcl/Tk 8.4.9. I've got a text widget with some elided text. A search returns the position L.C (properly), but a subsequent search starting at L.C for the same string fails to return L.C. However, a search with the option -elide does return L.C. The elided text is at the beginning of the line, a good 60 characters before the text in question. Here's the example: # -------------------------------- end toplevel .bfw pack [set t [text .bfw.t]] -expand 1 -fill both set elidetext "to elide" $t tag configure toelide -elide 1 $t insert end "random\n" $t insert end ${elidetext} toelide "something here missing instantiation\n" $t insert end ${elidetext} toelide "other text missing instantiation\n" puts [set res [$t search inst 1.0]] # prints 2.62 - proper starting point for 'inst' puts [$t search inst $res] # prints 3.57 - second occurrence of 'inst' puts [$t search -elide inst $res] # prints 2.62 - first occurrence of 'inst' # -------------------------------- end Am I doing something wrong? Is there some expectation I shouldn't have? I figured that all 3 searches should return 2.62. thanks, TJ
  • 2. Using $args recursively
    I'm calling a proc that receives its arguments through args. I shift some arguments off the $args list and then I call another proc recursively. Given: proc chkargs {args} {puts $args} a call to chkargs 1 2 3 yields 1 2 3 Now, I want to call chkargs from ch1 and the list of arguments is still in $args, for example: proc ch1 {args} { set args [ lindex $args 1 end ] chkargs $args } Now, calling ch1 1 2 3 4, will give me $args = {2 3 4} inside chkargs rather than 2 3 4 that I would like to have instead. What is the proper way to handle situations like these? K.
  • 3. console running as cgi-bin through tclhttpd
    Hi! Is there something around like a raw tcl cgi-bin to get a console on the system running a tclhttpd server? I am thinking of starting a shell script and piping the console commands to the shell while using the https connection to tclhttpd as transport protocol. A similar tool seems to be cgitelnet.pl. But this is starting again each time and therefore doesn't keep the state of the console. TIA Stefan
  • 4. Substitute variables into associative arrays
    Hello, Here is what I am trying to do: 1. I have a procedure that takes $myvar as one of its parameters. Inside the procedure, I have a foreach loop that goes something like this: foreach { var1 var2 } $myvar { # etc. } 2. Before passing $myvar into the procedure, I define it like this: set myvar { var1 var2 var3 var4 } What I found is that I cannot use direct variable substitution inside myvar (putting $var will result in TCL treating the $ as a character in a string and will display $var instead of its value). The way I work around it is when it comes to actually using the values (inside the procedure), I do [set var] to get my value. The problems start when I don't know if I want a given position to be a variable or just a constant. Is there a way to put the correct value while defining myvar and have to do [set x] inside the procedure? Thanks a lot for help!
  • 5. How can I get the values in a string pattern from the selected row in the tktable?
    XXXX@XXXXX.COM wrote: > Your help appreciated! set widget [ tktable .tkt ] set selection [ $widget curselection ] from the man page: THE SELECTION Table selections are available as type STRING. By default, the value of the selection will be the values of the selected cells in nested Tcl list form where each row is a list and each column is an element of a row list. You can change the way this value is interpreted by set- ting the -rowseparator and -colseparator options. For example, default Excel format would be to set -rowsepara- tor to "\n" and -colseparator to "\t". Changing these values affects both how the table sends out the selection and reads in pasted data, ensuring that the table should always be able to cut and paste to itself. It is possible to change how pastes are handled by editing the table library procedure tk_tablePasteHandler. This might be necessary if -selectioncommand is set. there is more relevant info in the manpage. uwe

Finding duplicate entries in a list

Postby Niv (KP) » Fri, 05 Sep 2008 15:52:20 GMT

I want to find all the duplicate entries in a list and write thm out.
So far I've done:

puts $f1 [lsort $mylist]
puts $f2 [lsort -unique $mylist]

and then compared them in Textpad. this was quick & easy, but surely
there's a way to do
what I want very simply and just write out the duplicat entries.

(This was to find any duplicate pin assignments in a large FPGA, which
there shouldn't be, I found 4!).

Regards, Kev P.

Re: Finding duplicate entries in a list

Postby Harm Olthof » Fri, 05 Sep 2008 16:29:30 GMT

maybe:
set l [list a a b c c]
foreach i $l {if {[info exists a($i)]} {puts $i} {set a($i) 1}}
a
c

The thing I don't like about this is that you build up an array. You will 
to unset it afterwards.

Harm









Re: Finding duplicate entries in a list

Postby Andreas Kupries » Fri, 05 Sep 2008 17:03:41 GMT

"Niv (KP)" < XXXX@XXXXX.COM > writes:


proc getdups {list} {
     array set tmp {}
     # Count items in tmp
     foreach item $list {
	lappend tmp($item) .
     }
     # Drop all items with count 1, they are unique
     foreach item [array names tmp] {
	if {[llength $tmp($item)] > 1} continue
        unset tmp($item)
     }
     # Return the remainder.
     return [array names tmp]
}

-- 
So long,
	Andreas Kupries < XXXX@XXXXX.COM >
			< http://www.**--****.com/ ;
	Developer @	< http://www.**--****.com/ ;
-------------------------------------------------------------------------------

Re: Finding duplicate entries in a list

Postby suchenwi » Fri, 05 Sep 2008 17:41:14 GMT



Best put this functionality in a proc, where the local array is
cleared up on exit.

Re: Finding duplicate entries in a list

Postby schlenk » Fri, 05 Sep 2008 18:24:02 GMT

If you have tcllib you can also use struct::set for this.

Michael

Re: Finding duplicate entries in a list

Postby Ron Fox » Fri, 05 Sep 2008 20:53:29 GMT


Would be simpler just to incr tmp($item)
then     if ($tmp($item) > 1} continue
At least that's how us histogramming mind-set folks think

-- 
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321

Re: Finding duplicate entries in a list

Postby Neil Madden » Fri, 05 Sep 2008 23:05:57 GMT



Another alternative to using an array is to sort the list and then 
traverse once checking for duplicates -- all dups are grouped in the 
sorted list, so detecting them is easy:

proc dups xs {
   set dups [list]
   set xs [lsort $xs]
   set len [expr {[llength $xs]-1}]
   for {set i 0} {$i < $len} {} {
     set x [lindex $xs $i]
     if {$x == [lindex $xs [incr i]]} {
       lappend dups $x
       # discard multiple repeats
       while {[lindex $xs $i] == $x} { incr i }
     }
   }
   return $dups
}

There doesn't seem to be any noticeable time difference between this and 
the array version, however.

-- Neil

Re: Finding duplicate entries in a list

Postby Andreas Kupries » Wed, 10 Sep 2008 15:03:38 GMT

Ron Fox < XXXX@XXXXX.COM > writes:


Only in 8.5. In 8.4 you still need an [info exists/incr/set] dance,
the use of lappend and unary counting was easier.


-- 
So long,
	Andreas Kupries < XXXX@XXXXX.COM >
			< http://www.**--****.com/ ;
	Developer @	< http://www.**--****.com/ ;
-------------------------------------------------------------------------------

Re: Finding duplicate entries in a list

Postby Sonya In Disguise » Fri, 12 Sep 2008 07:18:22 GMT





Another solution - this simply prints the duplicates:

proc prtDups { args } {
    set argc [ llength $args ]
    while { $argc > 1 } {
        foreach item $args {
            set group [ lsearch -all $args $item ]
            set argc [ llength $group ]
            if { $argc > 1 } {
                puts "duplicate = [ lindex $args [ lindex $group 0 ] ]"
                foreach i [ lsort -integer -decreasing $group ] {
                    set args [ lreplace $args $i $i ]
                }
                break;
            }
        }
    }
}

set foo [ list a b b c c d e f ]

eval prtDups $foo

Sonya



Similar Threads:

1.Command to find unique entries in a list

Hi,

            Whats the tcl command to get the unique entries in a list

Ex:

  list:

        green
        blue
        green
        blue


 to get the unique entries -- green, blue

We have uniq in Unix, The equivalent for uniq in TCL?

Please let me know

Thanks

2.Can any body tell me how to find duplicate lines in C

Can any body tell me how to find duplicate lines in C

i have tried to find using Binary tree and Text files but not suceeded
.

It works for Word but not for lines

please help me

you can write suggetion to my email :  XXXX@XXXXX.COM 

3.Duplicate elements in list

We have a list of elements, some are duplicates. We're trying to figure
out how to find the duplicate elements and increase a counter value by 1
for each instance of the element found. The list consists of lists with
two elements, the first being the incremental counter, the second being
the string sought. Example:

((1 "one") (1 "two") (1 "three") (1 "one") (1 "four") (1 "two"))

The result should be:

((2 "one") (2 "two") (1 "three") (1 "four"))


This is the function for adding 1 (incremental counter element):

(defun add-one-to-n (indata)
   (setq n (car indata))
   (setq outdata
     (cons (+ 1 n) (cdr indata))))

Any ideas on how we should go about achieving this?

Nick and Jake

4.duplicates in lambda-list, let bindings, etc.

I stumbled across this interesting discrepancy between implementations:

CLisp:

(let ((x 1) (x 2))
  x)
==> 2

ECL:

(let ((x 1) (x 2))
  x)
==> 1

SBCL:

(let ((x 1) (x 1))
  x)
...
; caught ERROR:
;   The variable X occurs more than once in the lambda list.
...

It is similar but different for actual lambda lists:

CLisp+ECL:

(funcall (lambda (x x) x) 1 2)
==> 2

SBCL again brings the above error.

Is this unspecified by the standard? SBCL's behaviour seems to make most
sense to me, in terms of catching errors but maybe there's a reason why
there should not be an error.

5.Duplicate elements in list (solved)

Svein Ove Aas wrote:
> Niklas Brunberg wrote:
> 
> 
>>We have a list of elements, some are duplicates. We're trying to figure
>>out how to find the duplicate elements and increase a counter value by 1
>>for each instance of the element found. The list consists of lists with
>>two elements, the first being the incremental counter, the second being
>>the string sought. Example:
>>
>>((1 "one") (1 "two") (1 "three") (1 "one") (1 "four") (1 "two"))
>>
>>The result should be:
>>
>>((2 "one") (2 "two") (1 "three") (1 "four"))
>>
>>
>>This is the function for adding 1 (incremental counter element):
>>
>>(defun add-one-to-n (indata)
>>   (setq n (car indata))
>>   (setq outdata
>>     (cons (+ 1 n) (cdr indata))))
>>
>>Any ideas on how we should go about achieving this?
> 
> 
> I'd use a hash table, much like this:
> 
> (defun count (list)
>   (let ((hash (make-hash-table)))
>     (dolist (el list)
>       (incf (gethash (cadr el) hash 0) (car el)))
>     (let (result)
>       (maphash (lambda (key val)
>                  (push (list val key) result))
>          hash)
>       result)))
> 

Thank you Svein Ove Aas! Solved most of our problems!

/ Niklas and Jakob

6. duplicates in lambda lists

7. "entry point not found" error

8. Entry point not found error



Return to tcl

 

Who is online

Users browsing this forum: No registered users and 30 guest