This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

how to remove duplicate items from a list

Help for those learning Tcl or writing their own scripts.
Post Reply
p
pranjal_ccna961
Voice
Posts: 5
Joined: Wed Feb 18, 2009 4:45 am

how to remove duplicate items from a list

Post by pranjal_ccna961 »

how to remove duplicate items from a list ? The list is receiving the input through a resultant loop.

If i use "lsort -unique", it gives me for that particlur loop run. If the same item is repeated next time in the loop, its not taking. Can anyone suggest me something. I figured out " lrmdups " solves this problem, but it is not working for the tcl version that i am using.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

What do you mean by "receiving the input through a resultant loop?" a code example would make things a lot clearer.
p
pranjal_ccna961
Voice
Posts: 5
Joined: Wed Feb 18, 2009 4:45 am

Post by pranjal_ccna961 »

puts -nonewline "Choose Input File Name: "
flush stdout
set myString [gets stdin]


set fp [open $myString r]
set data [read $fp]
close $fp


puts -nonewline "Choose Cell: "
flush stdout
set cell [gets stdin]


puts -nonewline "Output File: "
flush stdout
set output [gets stdin]


puts "Extracting nets for $cell .............."
after 1000

#split the file into lines
set data [split $data "\n"]

#total no of lines in the file
set datalength [llength $data]
###puts $datalength

#get the initial index for the cell line
set count -1
foreach line $data {
incr count
if {[regexp ".SUBCKT $cell" $line] == 1} {
set init $count
}
}
###puts $init

#sort the file from the cell line till end
set sortlist [lrange $data $init $datalength]

#get the final index for the cell line
set linecount -1
foreach newline $sortlist {
incr linecount
if {[regexp ".ENDS" $newline] == 1} {
set final $linecount
break
}
}
###puts $final


#adjust indexes
set firstindex [expr $init+1]
set endindex [expr $init+$final-1]

set cellsort [lrange $data $firstindex $endindex]
set uniquesort [lsort -unique $cellsort]
#puts $uniquesort


##sort out the nets for the cell
##this logic is to be improved yet to sort nets across lines
##in its current form sorts the nets just in every netlines

foreach netline $uniquesort {
if {[regexp {[A-Z][0-9]} $netline] == 1} {
set new [list $netline]
foreach nets $new {
set newnets [lrange $nets 1 3]
}
#puts $newnets
set out [open $output a+]
puts $out $newnets
close $out
}
}
puts "Extracted nets are in $output .............."
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

What's wrong with [lsort -unique]?
p
pranjal_ccna961
Voice
Posts: 5
Joined: Wed Feb 18, 2009 4:45 am

Post by pranjal_ccna961 »

[lsort -unique] is working to sort out duplicate names in the one line. But, if the loops encounter the same name in different line, its getting in the list. so the list still contains the duplicate names.
User avatar
arfer
Master
Posts: 436
Joined: Fri Nov 26, 2004 8:45 pm
Location: Manchester, UK

Post by arfer »

You could perhaps use the command lsearch in some way to maintain a list of unique names by only adding them if they don't already exist.

I haven't studied your code so I don't know exactly how to resolve your specific problem but below is an example of using a proc to parse a line (in this case a space delimited string of names). A global variable is maintained and consists of a list of unique names. The command lsearch is used to determine if a name within the line (after splitting into a list) already exists. If it already exists within the global variable then ignore it, if it doesn't exist then lappend to the global variable.

For each instance of a line simply call the proc pParseNames with the line as a single argument. Ultimately the global variable vNamesList will be an unsorted list of unique names taken from within all the lines.

Code: Select all

proc pParseNames {line} {
    global vNamesList
    set linelist [split $line]
    if {[info exists vNamesList]} {
        foreach name $linelist {
            if {[lsearch -exact $vNamesList $name] == -1} {
                lappend vNamesList $name
            }
        }
    } else {set vNamesList $linelist}
}
Untested but it looks OK. In any case I'm sure you understand my reasoning.
I must have had nothing to do
Post Reply