egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to remove duplicate items from a list

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
pranjal_ccna961
Voice


Joined: 18 Feb 2009
Posts: 5

PostPosted: Sun Mar 01, 2009 9:10 am    Post subject: how to remove duplicate items from a list Reply with quote

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
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Sun Mar 01, 2009 3:54 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
pranjal_ccna961
Voice


Joined: 18 Feb 2009
Posts: 5

PostPosted: Mon Mar 02, 2009 12:27 am    Post subject: Reply with quote

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
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Mon Mar 02, 2009 3:12 am    Post subject: Reply with quote

What's wrong with [lsort -unique]?
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
pranjal_ccna961
Voice


Joined: 18 Feb 2009
Posts: 5

PostPosted: Mon Mar 02, 2009 4:19 am    Post subject: Reply with quote

[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
View user's profile Send private message
arfer
Master


Joined: 26 Nov 2004
Posts: 436
Location: Manchester, UK

PostPosted: Tue Mar 03, 2009 6:20 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber