Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Manipulation

Status
Not open for further replies.

nvhuser

Programmer
Apr 10, 2015
48
DE
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






 

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!
 
Hi

Thank you for sharing the solution.

To make your code easier to read, please enclose it between [tt][ignore]
Code:
[/ignore][/tt] .. [tt][ignore]
[/ignore][/tt] TGML tags ( click the
help.png
button on the reply form's toolbar for better overview ). Additionally the preformatted input and output data you can enclose between [tt][ignore][pre][/ignore][/tt] .. [tt][ignore][/pre][/ignore][/tt] 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
redflag.png
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
 

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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top