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 names in the o/p file.

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 names in the o/p file.

Post by pranjal_ccna961 »

Hi,

I am giving a file (in) as input and sorting out the desired matched pattern in a different file(out). Since I am getting the output in loop, I am unable to remove the duplicate item.

set in [open filename_1 r]
set data [read $in]
close $in
set data [split $data "\n"]
foreach line $data {
if {[string match "X*" $line] == 1} {
set new [list $line]
foreach nets $new {
set newnets [lrange $nets 1 4]
lsort -unique $newnets
puts $newnets
}

Can anyone help me in this?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

I'd suggest you create a new, separate, list, and add desired rows there. This allows you to use lsearch to see whether the current data already exists within this list (if it does, just skip it).

One thing I noticed though; the code below is pretty pointless, as the list in $new will always contain one single list item (hence the loop will only run once).

Code: Select all

set new [list $line]
foreach nets $new {
NML_375
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Or, actually, since you use lists and glob-style matching, this could probably be made alot simpler...

Code: Select all

set newdata [lsort -unique [lsearch -all -inline -glob $data "X*"]]

#you do some data-mangling, so we'll do it here... I'm assuming each line is a valid tcl-list on it's own
foreach line $newdata {
 puts [lrange $line 1 4]
}
NML_375
Post Reply