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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String report 1

Status
Not open for further replies.

ShadowZA

Technical User
Jun 18, 2010
8
0
0
ZA
Hi Guys

Need some help on the following please.

I use a mine design program called SURPAC. In this i have a string file. I want to see if the first and last point of each segment correspond thus puts "CLOSED" if not puts "OPEN"

Attached find example of string data and code thus far:

String File:

boundary,17-Jun-10,,SSI_STYLES:styles.ssi
0, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
1, 10.892, -21.459, 0.000,
1, -162.709, 302.010, 0.000,
1, -223.824, 262.159, 0.000,
1, -136.770, 59.766, 0.000,
1, -122.366, 6.236, 0.000,
1, -3.786, -72.817, 0.000,
1, 10.892, -21.459, 0.000,
0, 0.000, 0.000, 0.000,
1, -421.239, -423.305, 0.000,
1, -246.459, -382.383, 0.000,
1, -236.858, -267.726, 0.000,
1, -287.289, -135.515, 0.000,
1, -472.920, -50.802, 0.000,
1, -552.803, -159.236, 0.000,
1, -515.960, -332.622, 0.000,
1, -422.239, -423.305, 0.000,
0, 0.000, 0.000, 0.000,
2, -50.128, -225.473, 0.000,
2, -3.786, -72.817, 0.000,
2, -122.366, 6.236, 0.000,
2, -242.310, -30.565, 0.000,
2, -287.289, -135.515, 0.000,
2, -236.858, -267.726, 0.000,
2, -50.128, -225.473, 0.000,
0, 0.000, 0.000, 0.000,
2, -677.461, -67.385, 0.000,
2, -515.960, -332.622, 0.000,
2, -552.803, -159.236, 0.000,
2, -472.920, -50.802, 0.000,
2, -394.755, 189.859, 0.000,
2, -678.461, -67.385, 0.000,
0, 0.000, 0.000, 0.000,
3, -209.483, -376.749, 0.000,
3, -50.128, -225.473, 0.000,
3, -236.858, -267.726, 0.000,
3, -246.459, -382.383, 0.000,
3, -209.483, -376.749, 0.000,
0, 0.000, 0.000, 0.000,
3, -136.770, 59.766, 0.000,
3, -223.824, 262.159, 0.000,
3, -394.755, 189.859, 0.000,
3, -472.920, -50.802, 0.000,
3, -287.289, -135.515, 0.000,
3, -242.310, -30.565, 0.000,
3, -122.366, 6.236, 0.000,
3, -136.770, 59.766, 0.000,
0, 0.000, 0.000, 0.000,
0, 0.000, 0.000, 0.000, END

Script :

set fimp [open "boundary.str" "r"]

gets $fimp line
gets $fimp line

set check 0

while {[gets ${fimp} line] >= 0} {

if {[string is double [lindex [split $line ","] 0]] && [lindex [split $line ","] 0] > 0 && [lindex [split $line ","] 0] != ""} {

set line [split $line ","]

switch -exact -- $check {

"0" {set next $line}
"1" {set prev $prev}

}

set prev $line
set check 1

} else {

set prev $prev
set next $next

if {[lindex $prev 1] == [lindex $next 1]} {

puts ""
puts "String [lindex $prev 0] Segment is CLOSED"

}

if {[lindex $prev 1] != [lindex $next 1]} {

puts ""
puts "String [lindex $next 0] first point \[[lindex $next 1];[lindex $next 2];[lindex $next 3]] is different from"
puts "String [lindex $prev 0] last point \[[lindex $prev 1];[lindex $prev 2];[lindex $prev 3]]"

puts "String [lindex $next 0] Segment is OPEN"
}

set check 0


}
}

close $fimp


Problems:

1. Need to specify not only that the string is "OPEN" but which segment it is.
2. Last segment gets duplicated as closed or open.
 
Just working my way through your code and trying to see what you're doing, I'm going to offer some "notes" as I go:
Code:
gets $fimp line
    gets $fimp line

    set check 0
    
    while {[gets ${fimp} line] >= 0} {
could be more concisely and maybe even more efficiently:
Code:
set check 0
set linelist [split [read $fimp] \n]
close $fimp
foreach line $linelist {

Code:
 if {[string is double [lindex [split $line ","] 0]] && [lindex [split $line ","] 0] > 0 && [lindex [split $line ","] 0] != ""}
How can the 0th element ever be a double and not !=""? Also you're doing a lot of redundant processing. I would sublist the line each time:
Code:
set elmlist [split $line ,]
Now the 0th element is either 0 or not, marking as I understand it, the segments. So while [lindex $elmlist 0]!=0 do something, otherwise do something else.

Try that, use code tags, and maybe you can have some specific questions.

_________________
Bob Rashkin
 
Hi Bob

Thanks for the reply.
I'm not a programmer and only try to get into the feel for things so that it can help me with my mine design program to ease the work.Also i enjoy it a lot and like to learn it more.

Thanks for the notes so far.

Just to maybe clear things up on what a string and what a segment is

1, 10.892, -21.459, 0.000,
1, -162.709, 302.010, 0.000,
1, -223.824, 262.159, 0.000,
1, -136.770, 59.766, 0.000,
1, -122.366, 6.236, 0.000,
1, -3.786, -72.817, 0.000,
1, 10.892, -21.459, 0.000,
0, 0.000, 0.000, 0.000,
1, -421.239, -423.305, 0.000,
1, -246.459, -382.383, 0.000,
1, -236.858, -267.726, 0.000,
1, -287.289, -135.515, 0.000,
1, -472.920, -50.802, 0.000,
1, -552.803, -159.236, 0.000,
1, -515.960, -332.622, 0.000,
1, -422.239, -423.305, 0.000,
0, 0.000, 0.000, 0.000,

As you can see from the above portion of data.
lindex 0 = String Number
lindex 1 = Y-coordinate
lindex 2 = X-coordinate
lindex 3 = Z-coordinate

In this case there are 2 segments. Thus String 1 Segment 1 is from 10.892 to 10.892. Due to the fact that the 1st and last point is the same ,it implies that the string,segment is closed.

Now in the next scenario we have another string 1, but this is now segment 2. And the Y-Coordinate is -421.239 to -422.239. Thus this implies that the string,segment is open.

So my result should read:

String 1 Segment 1 is CLOSED
String 1 Segment 2 is OPEN

Hope this clears it up and that the code can be modified to display this

Thanks

Martin Biewenga
 
Code:
set fimp [open "boundary.str" "r"]
gets $fimp; #read past header record
set linelst [split [read $fimp] \n]
close $fimp
set startx 0
set endx 0
set seglst {}
foreach line $linelst {
    set elmlst [split $line ,]
    if {[lindex $elmlst 0]==0 then {
       if {$endx==$startx} then {lappend seglst closed}
       else {lappend seglst open}
       incr segno
       set stepno 0
    else {
       incr stepno
       if {stpno==1} {set startx [lindex $elmlst 1]}
       set endx [lindex $elmlst 1]
    }
}
puts "There are [expr {[llength $seglst]-1}] line segments"
for {set i 1} {$i<[llength $seglst]} {incr i} {
   puts "segment $i is [lindex $seglst $i]"
}

_________________
Bob Rashkin
 
Thnx bong. But somewhere is a missing close brace. And i tried to close were i thought it should be but then syntax error occurs

 
This is without syntax errors:
Code:
set fimp [open "boundary.str" "r"]
gets $fimp; #read past header record
set linelst [split [read $fimp] \n]
close $fimp
set startx 0
set endx 0
set seglst {}

foreach line $linelst {
    set elmlst [split $line ,]
    if {[lindex $elmlst 0]==0} then {
      if {$endx==$startx} then {lappend seglst closed
      } else {lappend seglst open}
      incr segno
      set stepno 0 
    } else {
      incr stepno
      if {$stepno == 1} {set startx [lindex $elmlst 1]}
      set endx [lindex $elmlst 1]
    }
}

puts "There are [expr {[llength $seglst]-1}] line segments"
for {set i 1} {$i<[llength $seglst]} {incr i} {
   puts "segment $i is [lindex $seglst $i]"
}
 
sorry (and thanks to mikrom).

_________________
Bob Rashkin
 
Hi Guys

Thx yet again for the help Bong and now thx to you mikrom.

But there still is a problem.Sorry that it seems that you need to write the code for me,but from your code i have learnt alot,and it made my first code smaller and more precise in every sence.

Result from Bong's code:

There are 7 line segments
segment 1 is closed
segment 2 is open
segment 3 is closed
segment 4 is open
segment 5 is closed
segment 6 is closed
segment 7 is closed ##### This does not exist #####

Result needed:

String 1 Segment 1 is closed
String 1 segment 2 is open
String 2 segment 1 is closed
String 2 segment 2 is open
String 3 segment 1 is closed
String 3 segment 2 is closed

The issues:

In my initial code and this code. The last segment gets duplicated. IE there are 6 segments not 7.
Also it is perfect showing that all the segments from 1 to 7. But it is not tied to each other. Meaning for this exercise there are 2 segments of each string and not 7 segments of 1 string.

Thank you
 
Code:
set fimp [open "boundary.str" "r"]
gets $fimp; #read past header record
set linelst [split [read $fimp] \n]
close $fimp
set startx 0
set endx 0
set seglst {}

foreach line $linelst {
    set elmlst [split $line ,]
    if {[lindex $elmlst 0]==0 [red]&& $stepno>0[/red]} then {
      if {$endx==$startx} then {lappend seglst [red]{$strgno  closed}[/red]
      } else {lappend seglst open}
      incr segno
      set stepno 0
    } else {
      incr stepno
      if {$stepno == 1} {set startx [lindex $elmlst 1]}
      set endx [lindex $elmlst 1]
      [red]set strgno [lindex $elmlst 0][/red]
    }
}

puts "There are [expr {[llength $seglst]-1}] line segments"
[red]set strgno 0[/red]
for {set i 1} {$i<[llength $seglst]} {incr i} {
   [red]set lst1 [lindex $seglst $i]
   if {[lindex $lst1 0]>$strgno} {
      set strgno [lindex $lst1 0]
      set segno 1
   } else {
      incr segno
   }
   puts "string $strgno, segment $segno is [lindex $lst1 1]"[/red]
}

_________________
Bob Rashkin
 
Thanks Bob for the help but not the correct result.

Result Now:

There are 5 line segments
string open, segment 1 is
string open, segment 2 is closed
string open, segment 3 is
string open, segment 4 is closed
string open, segment 5 is closed

Result Needed:

There are 6 line segments
string 1 segment 1 is open
string 1 segment 2 is closed
string 2 segment 1 is open
string 2 segment 2 is closed
string 3 segment 1 is closed
string 3 segment 2 is closed
 
Sorry, I got caught by delayed substitution ({} vs "")
Code:
set f [open test1.txt r]
gets $f
set linelst [split [read $f] \n]
close $f
set startx 0
set endx 0
set seglst {}
set stepno 0
foreach line $linelst {
    set elmlst [split $line ,]
    if {[lindex $elmlst 0]==0 && $stepno>0} then {
      if {$endx==$startx} then {
         lappend seglst "$strgno  closed"
      } else {
         lappend seglst "$strgno open"
      }
      set stepno 0
    } elseif {[lindex $elmlst 0]!=0} {
      incr stepno
      if {$stepno == 1} {set startx [lindex $elmlst 1]}
      set endx [lindex $elmlst 1]
      set strgno [lindex $elmlst 0]
    }
}
puts "There are [expr {[llength $seglst]-1}] line segments"
set strgno 0
set segno 1
for {set i 1} {$i<[llength $seglst]} {incr i} {
   set lst1 [lindex $seglst $i]
   if {[lindex $lst1 0]>$strgno} {
      set strgno [lindex $lst1 0]
      set segno 1
   } else {
      incr segno
   }
   puts "string $strgno, segment $segno is [lindex $lst1 1]"
}

_________________
Bob Rashkin
 
Thanks a lot Bong it works as requested.

I only made one change but don't know if its the correct place to make it but after this it gave me the correct result.

change made:

for {set i 1} to
for {set i 0}

Before:

#####String and Segment missing#####
String 1 Segment 1 is open
String 2 Segment 1 is closed
String 2 Segment 2 is open
String 3 Segment 1 is closed
String 3 Segment 2 is closed

After:

String 1 Segment 1 is closed
String 1 Segment 2 is open
String 2 Segment 1 is closed
String 2 Segment 2 is open
String 3 Segment 1 is closed
String 3 Segment 2 is closed
 
Hi Bong

Please need your help.
Another issue is bugging me. With my existing code i only get to read the first string and segment.

###########
Data:

String Segment Direction # Points 2d Len 3d Len Area X Min X Max Y Min Y Max Z Min Z Max
1 1 Clockwise 7 911.8 911.8 31743.3 -72.8 302 -223.8 10.9 0 0
1 2 Clockwise 8 1083.2 1083.2 82541.5 -423.3 -50.8 -552.8 -236.9 0 0
1 15 1994.9 1994.9 114284.8 -423.3 302 -552.8 10.9 0 0

2 1 Clockwise 7 874.6 874.6 53985.4 -267.7 6.2 -287.3 -3.8 0 0
2 2 Clockwise 6 1257.7 1257.7 43506.6 -332.6 189.9 -677.5 -394.8 0 0
2 13 2132.4 2132.4 97492 -332.6 189.9 -677.5 -3.8 0 0

3 1 Clockwise 5 563.6 563.6 12850 -382.4 -225.5 -246.5 -50.1 0 0
3 2 Clockwise 8 1158.1 1158.1 81041.6 -135.5 262.2 -472.9 -122.4 0 0
3 13 1721.7 1721.7 93891.6 -382.4 262.2 -472.9 -50.1 0 0


###########
Code:

set fimp [open "result_extent.csv" "r"]
gets $fimp
set linelst [split [read $fimp] \n]
close $fimp

set _srchk 0

foreach line $linelst {

set line [split $line ","]
set chk [lindex $line 0]

if {${_srchk} == 1} {

set _string [lindex $line 0]
set _segment [lindex $line 1]
set _direction [lindex $line 2]
set _points [lindex $line 3]
set _2dlen [lindex $line 4]
set _3dlen [lindex $line 5]
set _area [lindex $line 6]
set _xmin [lindex $line 7]
set _xmax [lindex $line 8]
set _ymin [lindex $line 9]
set _ymax [lindex $line 10]
set _zmin [lindex $line 11]
set _zmax [lindex $line 12]

set _srchk 0
}

if {${chk} == "String "} {

set _srchk 1
}

}

if {${_direction} == "Clockwise"} {

puts "String ${_string} Segment ${_segment} is Closed and ${_direction}"

} else {

puts "String ${_string} Segment ${_segment} is Open"

}

puts "Points = ${_points}, 2DLength = ${_2dlen}m, 3DLength = ${_3dlen}m, Area = ${_area}m²"
puts "Ymin = ${_ymin} Ymax = ${_ymax}"
puts "Xmin = ${_xmin} Xmax = ${_xmax}"
puts "Zmin = ${_zmin} Zmax = ${_zmax}"
puts ""

###########
Result:

String 1 Segment 1 is Open
Points = 7, 2DLength = 911.8m, 3DLength = 911.8m, Area = 31743.3m²
Ymin = -223.8 Ymax = 10.9
Xmin = -72.8 Xmax = 302
Zmin = 0 Zmax = 0
 
There's always a problem with data, isn't there?
It looks like a data record has 13 elements. Is this guaranteed?
For each string, it appears that there is a summation (over segments) record. Is this guaranteed?
Is there always a blank line separating strings?

So what is it you want to do? I know I could read your code but that's usually not fruitful.

_________________
Bob Rashkin
 
I got it sorted thank you. Redid the code differently and was more successful..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top