| View previous topic :: View next topic |
| Author |
Message |
pranjal_ccna961 Voice
Joined: 18 Feb 2009 Posts: 5
|
Posted: Sun Mar 01, 2009 9:10 am Post subject: how to remove duplicate items from a list |
|
|
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. |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Sun Mar 01, 2009 3:54 pm Post subject: |
|
|
What do you mean by "receiving the input through a resultant loop?" a code example would make things a lot clearer. _________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
pranjal_ccna961 Voice
Joined: 18 Feb 2009 Posts: 5
|
Posted: Mon Mar 02, 2009 12:27 am Post subject: |
|
|
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 .............." |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
|
| Back to top |
|
 |
pranjal_ccna961 Voice
Joined: 18 Feb 2009 Posts: 5
|
Posted: Mon Mar 02, 2009 4:19 am Post subject: |
|
|
| [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. |
|
| Back to top |
|
 |
arfer Master

Joined: 26 Nov 2004 Posts: 436 Location: Manchester, UK
|
Posted: Tue Mar 03, 2009 6:20 am Post subject: |
|
|
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: |
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 |
|
| Back to top |
|
 |
|