PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] How do I read a coordinate csv file and break into E,N,Z and Station?

 August 29, 2022     csv, list, tcl     No comments   

Issue

How do I read a csv file and put the Northing, Easting, RL and StationName into separte variables I can call later in the code?

...line3="$STN1"  
line4="$E1"  
line5="$N1"  
line6="$Z1"  
line7="$STN2"  
line8="$E2"  
line9="$N2"  
line10="$Z2"...

I am using a program called Surpac which uses TCL/SCL(Surpac Command Language) as an internal language to run macros, so I'm not sure how to get tcllib CSV to work for this, so I would like to know if there is a more native version.

I have a file as so;

Header (ignore)  
Start line (ignore)  
stringno, Northing, Easting, RL, StationName, other, crap, I, don't, care, about  
stringno, Northing, Easting, RL, StationName, other, crap, I, don't, care, about  
SegmentBreak (0,0,0,0)  
Stringno, Northing, Easting, RL, StationName,...  
SegmentBreak (0,0,0,0)  
End of file (0, 0.000, 0.000, 0.000, END)  

Sample file;

Header, x, y, z  
0, 0.000, 0.000, 0.000, 0  
1, 1724649.512, 124030.344, 139.540, 120100-01,58,0.0159,0.0128,0.0024   
1, 1724767.644, 125844.370, 107.213, 12100-13,68,0.0108,0.009,0.0026  
.....   
1, 1724815.346, 1285816.668, 99.484, 12000-14,70,0.0113,0.0092,0.0026    
1, 1724797.046, 125785.089, 90.848, 12000-16,87,0.0127,0.009,0.0027  
0, 0.000, 0.000, 0.000,    
1, 1725028.806, 125903.122, -1318.821, SD152,2038,0.0154,0.0124,0.0097  
1, 1725028.899, 125901.241, -1322.790, 857002,2039,0.0156,0.0125,0.0098  
0, 0.000, 0.000, 0.000,  
0, 0.000, 0.000, 0.000, END  

As far as programming goes, I'm a hack and need help, so I have this so far. It's crap, but a start.

set stnfile [open $csvfile r]  
gets $stnfile header  
gets $stnfile nosence  
gets $stnfile STN1  
gets $stnfile STN2..... 

No surprise, this just gives me each line as as variable: STNx


Solution

If you can't get tcllib installed and working, for simple data like this, there's split to break a line up into a list:

set stnfile [open $csvfile r]
# Read and discard the first two lines
gets $stnfile
gets $stnfile
# Read the rest in a loop
while {[gets $stnfile line] > 0]} {
    # Split on comma and assign the first five elements to variables
    lassign [split $line ,] stringno northing easting rl stationname
    # Exit on END - beware of the leading space!
    if {$stationname eq " END"} { break }
    # Check for a segment break
    if {$stringno == 0 && $northing == 0 && $easting == 0 && $rl == 0} {
        # Do something with a break line?
    } else {
        # Do something else with a regular line?
    }
}
close $stnfile


Answered By - Shawn
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,207,318

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing