INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Jobs from Indeed

Data Manipulation

Data Manipulation

(OP)
Hello everyone, I am a beginner in TCL, I would like to have some help dealing with data manipulation. I have the following text stored in a file:
#
#2345678$2345678$2345678$2345678$2345678$2345678$2345678$2345678$2345678
#
10.0000000.0000000.000000 230.7500000.5000000.0000000.812500
21.5000000.0000000.000000 262.0625000.3750000.0000000.457031
33.0000000.0000000.000000 253.3750000.2500000.0000000.203125
44.5000000.0000000.000000 304.6875000.1250000.0000000.050781
56.0000000.0000000.000000 216.0000000.0000000.0000000.000000
60.0000001.0000000.000000 230.7500000.5000000.0000000.812500
71.5000001.0000000.000000 291.5000001.2500000.0000000.062500
83.0000001.0000000.000000 322.6250000.9375000.0000000.144531
94.5000001.0000000.000000 244.1250001.0000000.0000000.140625
106.0000001.0000000.000000 216.0000000.0000000.0000001.000000
121.5000002.0000000.000000 222.2500002.0000000.0000000.562500
133.0000002.0000000.000000 283.1875001.5000000.0000000.285156
144.5000002.0000000.000000 244.1250001.0000000.0000001.140625
171.5000003.0000000.000000 222.2500002.0000000.0000001.562500
183.0000003.0000000.000000 222.2500002.0000000.0000001.562500

it consists into information stored in 8-digit format.
I already wrote the following script to read the txt file and to filter the information stored:

#
set filename03 "slot0to1.txt";
set open_file03 [open $filename03 "r"]
set read_file03 [read -nonewline $open_file03]
set split_file03 [split $read_file03 "\n"]
set i 1
foreach line $split_file03 {
set ID1 [string range $line 1 7 ]
set X1 [string range $line 8 15 ]
set Y1 [string range $line 16 23]
set Z1 [string range $line 24 31 ]
set ID2 [string range $line 32 39 ]
set X2 [string range $line 40 47 ]
set Y2 [string range $line 48 55 ]
set Z2 [string range $line 56 63 ]
set d1_2 [string range $line 64 71]
incr i
}

Now, I would like check if there are duplicates in the $ID2 (colums 32 to 39) and delete them, leaving the lower number in $ID1.

The output should looks like:

10.0000000.0000000.000000 230.7500000.5000000.0000000.812500
21.5000000.0000000.000000 262.0625000.3750000.0000000.457031
33.0000000.0000000.000000 253.3750000.2500000.0000000.203125
44.5000000.0000000.000000 304.6875000.1250000.0000000.050781
56.0000000.0000000.000000 216.0000000.0000000.0000000.000000
71.5000001.0000000.000000 291.5000001.2500000.0000000.062500
83.0000001.0000000.000000 322.6250000.9375000.0000000.144531
94.5000001.0000000.000000 244.1250001.0000000.0000000.140625
121.5000002.0000000.000000 222.2500002.0000000.0000000.562500
133.0000002.0000000.000000 283.1875001.5000000.0000000.285156

Thank you very much






RE: Data Manipulation

(OP)

Hello, thanks to Feherke I was able to solve the problem described above.
Here is the solution, this will be a good contribution for this forum:

set filename01 "slot0to1.txt";
set open_file01 [open $filename01 "r"]
set read_file01 [read -nonewline $open_file01]
set split_file01 [split $read_file01 "\n"]
set i 1

set filename02 "aux.txt";
set file02 [open $filename02 w];

foreach line $split_file01 {
set ID1 [string range $line 1 7 ]
set X1 [string range $line 8 15 ]
set Y1 [string range $line 16 23]
set Z1 [string range $line 24 31 ]
set ID2 [string range $line 32 39 ]
set X2 [string range $line 40 47 ]
set Y2 [string range $line 48 55 ]
set Z2 [string range $line 56 63 ]
set d1_2 [string range $line 64 71]
incr i
puts $file02 "[format "%s %f %f %f %s %f %f %f %f" $ID1 $X1 $Y1 $Z1 $ID2 $X2 $Y2 $Z2 $d1_2]"
}

close $file02

set file [open $filename02 r]
while { [gets $file line] >= 0 } {
lappend matrix $line
}
close $file

array set max {}
foreach line $matrix {
if {[array get max [lindex $line 4]] == "" || [lindex [array get max [lindex $line 4]] 1] > [lindex $line 8]} {
set max([lindex $line 4]) [lindex $line 8]
}
}

foreach line $matrix {
if {[lindex [array get max [lindex $line 4]] 1] == [lindex $line 8]} {
puts $line
array unset max [lindex $line 4]
}
}
file delete "./aux.txt"

#####################

Contributions would be welcome!

RE: Data Manipulation

Hi

Thank you for sharing the solution.

To make your code easier to read, please enclose it between [code] .. [/code] TGML tags ( click the button on the reply form's toolbar for better overview ). Additionally the preformatted input and output data you can enclose between [pre] .. [/pre] tags. That way they will be rendered with monospaced font and indentations will be preserved.

( Sadly there is no way to edit your post, but you can post the same again with TGML markup, then click the Red Flag this post link at the bottom of your original post and in the message area ask forum management to remove your old post ( and this post by me, as will become pointless ). )

Feherke.
feherke.ga

RE: Data Manipulation

(OP)

Hello, thanks to Feherke I was able to solve the problem described above.
Here is the solution, this will be a good contribution for this forum:

CODE

set filename01 "slot0to1.txt";
set open_file01 [open $filename01 "r"]
set read_file01 [read -nonewline $open_file01]
set split_file01 [split $read_file01 "\n"]
set i 1

set filename02 "aux.txt";
set file02 [open $filename02 w];

foreach line $split_file01 {
      set ID1 [string range $line 1 7 ]
      set X1 [string range $line 8 15 ]
      set Y1 [string range $line 16 23]
      set Z1 [string range $line 24 31 ]
      set ID2 [string range $line 32 39 ]
      set X2 [string range $line 40 47 ]
      set Y2 [string range $line 48 55 ]
      set Z2 [string range $line 56 63 ]
      set d1_2 [string range $line 64 71]
      incr i
      puts $file02 "[format "%s %f %f %f %s %f %f %f %f" $ID1 $X1 $Y1 $Z1 $ID2 $X2 $Y2 $Z2 $d1_2]"
}

close $file02

set file [open $filename02 r]
while { [gets $file line] >= 0 } {
      lappend matrix $line
}
close $file

array set max {}
foreach line $matrix {
       if {[array get max [lindex $line 4]] == "" || [lindex [array get max [lindex $line 4]] 1] > [lindex $line 8]} {
                set max([lindex $line 4]) [lindex $line 8]
       }
}

foreach line $matrix {
      if {[lindex [array get max [lindex $line 4]] 1] == [lindex $line 8]} {
                puts $line
                array unset max [lindex $line 4]
      }
}
file delete "./aux.txt"

##################### 

Contributions would be welcome!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close