jeretjordan has asked for the wisdom of the Perl Monks concerning the following question:
This my very first week using Perl. My question is how and where would I add in a list of headers that I need to show in my out file? This is the list of headers I want outputted ETXLinkID,TranscomLinkID,TranscomLen,TranscomLenFrac,EtxLenFrac,EtxLen
#!perl use ; $, = ' '; # set output field separator $\ = "\n"; # set output record separator $TOLERANCE = 0.000001; ### ### ETX Link ID read fields ### ### Field Order: 0 LINK_ID ** WANTED ### Field Order: 1 Link_Name ### Field Order: 2 Native_Link_ID ### Field Order: 3 Owner_Name ### Field Order: 4 Description ### Field Order: 5 Link_Type ### Field Order: 6 Direction ** WANTED ### Field Order: 7 Begin_State ### Field Order: 8 End_State ### Field Order: 9 Begin_Facility ** WANTED ### Field Order: 10 End_Facility ### Field Order: 11 Begin_Point_Name ### Field Order: 12 End_Point_Name ### Field Order: 13 Begin_Point_ID ### Field Order: 14 End_Point_ID ### Field Order: 15 BEGIN_EXT_NAME ### Field Order: 16 END_EXT_NAME ### Field Order: 17 BEGIN_LAT ** WANTED ### Field Order: 18 BEGIN_LON ** WANTED ### Field Order: 19 END_LAT ** WANTED ### Field Order: 20 END_LON ** WANTED ### Field Order: 21 Status ### Field Order: 22 LastUpdated ### Field Order: 23 LENGTH Meters ### Transcom Link ID read fields ### ### Field Order: 0 TranscomLinkID ** WANTED ### Field Order: 1 LinkName ### Field Order: 2 TranscomRoadName ** WANTED ### Field Order: 3 Direction ** WANTED ### Field Order: 4 StartLat ** WANTED ### Field Order: 5 StartLon ** WANTED ### Field Order: 6 EndLat ** WANTED ### Field Order: 7 EndLon ** WANTED ### Field Order: 8 Length ** WANTED Meters #### Lookup Table #### ### Field Order: 0 SourceFacilityName ### Field Order: 1 LocalFacilityName with direction #### $icnt = 0; $fname = $ARGV[0]; ##$fname = 'Transcom_XREF.txt'; ### ### LookUP DATA Read for Cross Reference ### my $csv = Text::CSV_XS->new ({ binary => 1 }) or die "Cannot use CSV: ".Text::CSV_XS->error_diag (); open my $fh, "<:encoding(utf8)", $fname or die "cannot Open $fname +: $!"; line: while (my $row = $csv->getline ($fh)) { if ($icnt == 0) { # Skip Header Record $icnt++; next; } @Fld = @$row; $ky = $Fld[1]; $LookUp{$ky} = $Fld[0]; # Save Translation table from Transcom + RoadName field to ETX facility name $Assgn{$Fld[0]} = $ky; # Save Assignment of Transcom value to + KEY ## print "LOOKUP..ky..$ky..LookUp..$LookUp{$ky}..."; ## print "LOOKUP..ky..$ky..Assgn{$Fld[0]}..$Assgn{$Fld[0]}..."; + next line; } # **End of line: Loop over all records in file close($fh); ### $icnt = 0; ##$fname = $ARGV[0]; $fname = $ARGV[1]; ### ### TRANSCOM DATA Read ### my $csv = Text::CSV_XS->new ({ binary => 1 }) or die "Cannot use CSV: ".Text::CSV_XS->error_diag (); open my $fh, "<:encoding(utf8)", $fname or die "cannot Open $fname : +$!"; line: while (my $row = $csv->getline ($fh)) { if ($icnt == 0) { # Skip Header Record $icnt++; next; } @Fld = @$row; $bt = $Fld[4]; ## StartLat $et = $Fld[6]; ## EndLat $bl = $Fld[5]; ## StartLong $el = $Fld[7]; ## EndLong ##print "TXDOT..BEGIN...fld0..$Fld[0]..f2..$Fld[2]....f7..$Fld[7]..bt. +.$bt..et..$et..bl..$bl..el..$el..$#Fld.."; $dy = ($et - $bt); $dx = ($el - $bl); $dist = sqrt($dx*$dx +$dy*$dy); if( $dist < $TOLERANCE ) { # Check to make sure segment is v +alid next line; } $vx = $dx/$dist; # Normalize the Basis Vector $vy = $dy/$dist; ##print "TXDOT..B4..ckkey...fld0..$Fld[0]..f2..$Fld[2]....f7..$Fld[7]. +.f1..$Fld[1]..f3..$Fld[3]...#fld..$#Fld.."; $key = $Assgn{$Fld[0]}; ##print "TRANSCOM..key...$key...TRANSCOM Link..$Fld[0].."; ## } ## if( $key eq "-1 : -1" ) { ## print "TXDOT..NO MATCH..$key..ckkey..$ckkey..fld0..$Fld[0]..f2 +..$Fld[2]..f7..$Fld[7]..f1..$Fld[1]..#fld..$#Fld.."; ## print "-1,$Fld[0],-1"; ## } $id{$key}++; $np = $id{$key}-1; $lnkID{$key, $np} = $Fld[0]; # Store Transcom LinkID $blat{$key, $np} = $bt; # Store Beginning Latitude $blng{$key, $np} = $bl; # Store Beginning Longtitude $elat{$key, $np} = $et; # Store Ending Latitude $elng{$key, $np} = $el; # Store Ending Longitude $lngth{$key, $np} = $Fld[8]; # Store length $vx0{$key, $np} = $vx+0.0; $vy0{$key, $np} = $vy+0.0; $tdst{$key, $np} = $dist+0.0; next line; } # **End of line: Loop over all records in file close($fh); ### ### ETX Link ID Read File (in Smartnet data format) ### $icnt = 0; ##$fname = $ARGV[1]; $fname = $ARGV[2]; my $csv = Text::CSV_XS->new ({ binary => 1 }) or die "Cannot use CSV: ".Text::CSV_XS->error_diag (); open my $fh, "<:encoding(utf8)", $fname or die "cannot Open $fname : +$!"; line: while (my $row = $csv->getline ($fh)) { if ($icnt == 0) { $icnt++; next; } @Fld = @$row; $ddr = $Fld[6]; # Get direction ONLY from field $ddr =~ m/(Frontage Rd)/; $fr = ""; if( $1 ne "" ) { $fr = "FR"; } if( index($ddr,"3") >= 0 ) { $dr = "EB"; } elsif( index($ddr,"4") >= 0 ) { $dr = "WB"; } elsif( index($ddr,"1") >= 0 ) { $dr = "NB"; } elsif( index($ddr,"2") >= 0 ) { $dr = "SB"; } else { $dr = ""; } ##print "ETX dr...$dr..ETXlnkid..$Fld[0]..."; #JAW $ns = (@tok = split(/ /, $ky, -1)); ## $ns = (@tok = split(/,/, $i, -1)); #JAW $nm = $tok[0]; $nm = $Fld[9]; # Get BeginField ONLY. t +his is the link facility name #JAW if( index($ky,"STHY 5 ") >= 0 ) { #JAW $nm = "K Avenue"; #JAW } #JAW else { #JAW $nm .= $tok[1]; #JAW } #JAW $nm .= $fr; $nmtch = $dr . $nm; # direction and facility + name forms unique key #JAW $nmtch =~ s/ //g; # do not remove spaces $key = $nmtch; # save Roadway ADJUSTED (Begin_Facility) ## $key = $Fld[9]; # save Roadway (Begin_Facility) ## if ( exists $LookUp{$Fld[9]} ) { # CHECK LOOKUP KEY : TxDOT ke +y field with SmartNET Begin Facility ## $key = $LookUp{$Fld[9]}; # save Roadway Key ## } ##print "ETX..key...$key..ETXlnkid..$Fld[0]..."; $sid{$key}++; $np = $sid{$key}-1; $slnkID{$key, $np} = $Fld[0]; # Store ETX LINK_ID $sblat{$key, $np} = $Fld[17]; # Store BEGIN_LAT $sblng{$key, $np} = $Fld[18]; # Store BEGIN_LON $selat{$key, $np} = $Fld[19]; # Store END_LAT $selng{$key, $np} = $Fld[20]; # Store END_LON $slnklen{$key, $np} = $Fld[23]; # Store ETX link length $dir{$key, $np} = $tmp; # Store Psuedo-Direction from ETX next line; } # **End of line: Loop over all records in file close($fh); ########### ########### ## Main Program - Calculate Segments over Links ########### ########### ####### ####### ####### Process the data ####### ####### foreach $i (keys %id) { # $i is the ETXFacilit +y Name ##print "The i..$i..this is ETXFacility Name"; ## $nv = (@tok = split(/:/, $i, -1)); # Split Roadway : Directi +on for TXDOT foreach $j (keys %sid) { # $j is the direction + + ETXFacility Name ##print "The j..$j..this is direction + ETXFacility Name"; ## $iv = ( $i eq $j); ##print "MATCHING..CHK..i..$i..j..$j.....lnkID.$lnkID{$i,0}..slnkID... +$slnkID{$j,0}..."; if( ($il=index($j,$i)) >= 0 ) { # if( $i eq $j ) { # check if Roadway Name +is within ETX Begin_Facility ##print "MATCHING..CHK..i..$i..j..$j.....lnkID.$lnkID{$i,0}..slnkID... +$slnkID{$j,0}..."; $nb = $id{$i}; # number of Transcom lin +ks under this LocalFacility name ##print "nb..$nb"; for($k=0; $k<$nb; $k++) { # Loop Transcom segments ( +BASIS) $ns = $sid{$j}; # number of ETX links f +or the direction + Local Facility Name ##print "ns..$ns"; for($l=0; $l<$ns; $l++) { # Loop ETX segments $ky = $slnkID{$j, $l} . ":" . $lnkID{$i, $k} . ":" . $lngth{ +$i, $k} . ":" . $slnklen{$j, $l}; # Build Key for holding results ET +XLinkID:TranscomLinkID:TranscomLinkLength $ksy = $slnkID{$j, $l}; # Save ETX Ke +y for holding results ETXLinkID ##print "ky..$ky.....ksy...$ksy"; ## ## First check BEGINNING point within segment ## $dy = ($sblat{$j, $l} - $blat{$i, $k}); $dx = ($sblng{$j, $l} - $blng{$i, $k}); $dist = sqrt($dx*$dx +$dy*$dy); if( $dist < $TOLERANCE ) { last; } $vx1 = $dx/$tdst{$i, $k}; # Normalize the 1st Vector to +check RELATIVE to DISTANCE OF BASIS $vy1 = $dy/$tdst{$i, $k}; ##print "j..$j..l..$l..k..$k"; ##print "check BEGINNING point within segment"; ##print "sblat{j, l}..$sblat{$j, $l}..sblng{j, l}..$sblng{$j, $l}..bla +t{i, k}..$blat{$i, $k}..blng{i, k}..$blng{$i, $k}..tdst{i, k}..$tdst{ +$i, $k}"; ##print "for dotp1..dy..$dy..dx..$dx..dist..$dist..vx1..$vx1..vy1..$vy +1..vx0..$vx0{$i, $k}..vy0..$vy0{$i, $k}"; $dotp1 = $vx1*$vx0{$i, $k} + $vy1*$vy0{$i, $k}; # Find di +stance along vector for Beginning point ##print "dotp1...$dotp1"; ## ## Now check ENDING point within segment ## $dy = ($selat{$j, $l} - $blat{$i, $k}); $dx = ($selng{$j, $l} - $blng{$i, $k}); $dist = sqrt($dx*$dx +$dy*$dy); if( $dist < $TOLERANCE ) { last; } $vx2 = $dx/$tdst{$i, $k}; # Normalize the 2nd Vector to +check RELATIVE to DISTANCE OF BASIS $vy2 = $dy/$tdst{$i, $k}; ##print "check ENDING point within segment"; ##print "selat{j, l}..$selat{$j, $l}..selng{j, l}..$selng{$j, $l}..bla +t{i, k}..$blat{$i, $k}..blng{i, k}..$blng{$i, $k}"; ##print "for dotp2..dy..$dy..dx..$dx..dist..$dist..vx2..$vx2..vy2..$vy +2"; $dotp2 = $vx2*$vx0{$i, $k} + $vy2*$vy0{$i, $k}; # Find di +stance along vector for Ending point ##print "dotp2...$dotp2"; ## ## Now check DOT PRODUCTS to find where along the basis vector line ## if( $dotp1 > 1.0 && $dotp2 > 1.0 ) { # Both point +s beyond END next; } elsif( $dotp1 < 0.0 && $dotp2 < 0.0 ) { # Both point +s before BEGIN next; } elsif( $dotp2 < $dotp1 ) { # Wrong Direction for Vecto +r - SWITCH DOT VALUES **** $dtmp = $dotp2; $dotp2 = $dotp1; $dotp1 = $dtmp; } ##print "dotp1...$dotp1..dotp2...$dotp2"; if( $dotp1 >= 0.0 ) { if( $dotp2 <= 1.0 ) { # Entire Segment is Completely i +nside $snC2Clen{$ky} = ($dotp2 - $dotp1); } elsif( $dotp2 > 1.0 ) { # Segment is Beyond Endpoint - + Truncate $snC2Clen{$ky} = (1.0 - $dotp1); } else { # Segment NOT FOUND in Basis V +ector $snC2Clen{$ky} = -1.0; } } elsif( $dotp1 <= 0.0 ) { if( $dotp2 >= 1.0 ) { # Segment Covers Entire Basis +Vector $snC2Clen{$ky} = 1.0; } elsif( $dotp2 > 0.0 ) { # Segment Starts before Basis +Vector (dotp2 - 0.0) $snC2Clen{$ky} = $dotp2; } else { # Segment NOT FOUND in Basis V +ector $snC2Clen{$ky} = -1.0; } } ##print "snC2Clen{ky}...$snC2Clen{$ky}"; ## ## NOW FIND Percent of ETX LinkID that is being used - ## ## ## Find NEW ETX Basis Vector ## $dy = ($selat{$j, $l} - $sblat{$j, $l}); $dx = ($selng{$j, $l} - $sblng{$j, $l}); $sdist = sqrt($dx*$dx +$dy*$dy); if( $sdist < $TOLERANCE ) { $sdist = 1.0 } $vxS = $dx/$sdist; # Normalize the SmartNET Vector $vyS = $dy/$sdist; ## ## Now find BEGINNING Transcom Link Segment relative to ETX Basis Vect +or ## $dy = ($blat{$i, $k} - $sblat{$j, $l}); $dx = ($blng{$i, $k} - $sblng{$j, $l}); $dist = sqrt($dx*$dx +$dy*$dy); if( $dist < $TOLERANCE ) { $dist = 1.0 } $vx1 = $dx/$sdist; # Normalize the 1st Vector to check R +ELATIVE to SmartNET BASIS $vy1 = $dy/$sdist; $dotp1 = $vx1*$vxS + $vy1*$vyS; # Find distance along vec +tor for Beginning point ## ## Now find ENDING Transcom Link segment relative to ETX Basis Vector ## $dy = ($elat{$i, $k} - $sblat{$j, $l}); $dx = ($elng{$i, $k} - $sblng{$j, $l}); $dist = sqrt($dx*$dx +$dy*$dy); if( $dist < $TOLERANCE ) { $dist = 1.0 } $vx2 = $dx/$sdist; # Normalize the 2nd Vector to check R +ELATIVE to DISTANCE OF BASIS $vy2 = $dy/$sdist; $dotp2 = $vx2*$vxS + $vy2*$vyS; # Find distance along vec +tor for Ending point ## ## Now check DOT PRODUCTS to find where along the basis vector line ## if( $dotp2 < $dotp1 ) { # Wrong Direction for Vector - + SWITCH DOT VALUES **** $dtmp = $dotp2; $dotp2 = $dotp1; $dotp1 = $dtmp; } if( $dotp1 >= 0.0 ) { if( $dotp2 <= 1.0 ) { # Entire Segment is Completely i +nside $ln = ($dotp2 - $dotp1); if( $ln > 0 ) { $snPLen{$ky} += ($dotp2 - $dotp1); $snLen{$ksy} += ($dotp2 - $dotp1); } } elsif( $dotp2 > 1.0 ) { # Segment is Beyond Endpoint - + Truncate $ln = (1.0 - $dotp1); if( $ln > 0 ) { $snPLen{$ky} += (1.0 - $dotp1); $snLen{$ksy} += (1.0 - $dotp1); } } else { # Segment NOT FOUND in Basis V +ector $snPLen{$ky} = -1.0; $snLen{$ksy} = -1.0; } } elsif( $dotp1 <= 0.0 ) { if( $dotp2 >= 1.0 ) { # Segment Covers Entire Basis +Vector $snPLen{$ky} = 1.0; $snLen{$ksy} = 1.0; } elsif( $dotp2 > 0.0 ) { # Segment Starts before Basis +Vector (dotp2 - 0.0) $ln = $dotp2; if( $ln > 0 ) { $snPLen{$ky} += $dotp2; $snLen{$ksy} += $dotp2; } } else { # Segment NOT FOUND in Basis V +ector $ln = $dotp2; if( $ln > 0 ) { $snPLen{$ky} += $dotp2; $snLen{$ksy} += $dotp2; } ## $snLen{$ksy} = -2.0; } } } # End of L loop } # End of K loop } # End of tok[0] Condition (Same Roadway) } # End of J loop (Roadway) } # End of I loop (Roadway:Direction) foreach $is (sort keys %snC2Clen) { $nv = (@tok = split(/:/, $is, -1)); # Split SnLinkID : TxLINKI +D if( $snPLen{$is} > 0 ) { ## printf "%-s,%-s,%.5lf,%.5lf,%.5lf\n", $tok[0], $tok[1], $snC2Cle +n{$is}, $snPLen{$is}, $snLen{$tok[0]}; printf "%-s,%-s,%.5lf,%.5lf,%.5lf,%.5lf\n", $tok[0], $tok[1], $t +ok[2], $snC2Clen{$is}*$tok[2], $snPLen{$is}, $tok[3]; } ## printf "%-s,%-s,%.5lf\n", $tok[0], $tok[1], $snC2Clen{$is}; ## printf "%-s,%-s,%.5lf\n", $is, $snLen{$is}; } exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inserting a header into outfile
by kennethk (Abbot) on Mar 14, 2017 at 17:01 UTC | |
by jeretjordan (Initiate) on Mar 15, 2017 at 02:40 UTC | |
by kennethk (Abbot) on Mar 15, 2017 at 14:45 UTC | |
by Anonymous Monk on Mar 14, 2017 at 17:19 UTC | |
|
Re: Inserting a header into outfile -- a long one
by Discipulus (Canon) on Mar 14, 2017 at 21:18 UTC | |
|
Re: Inserting a header into outfile
by Anonymous Monk on Mar 14, 2017 at 16:54 UTC | |
|
Re: Inserting a header into outfile
by jeretjordan (Initiate) on Mar 14, 2017 at 17:05 UTC | |
by haukex (Archbishop) on Mar 14, 2017 at 18:40 UTC | |
by kennethk (Abbot) on Mar 14, 2017 at 17:15 UTC | |
| |
by Anonymous Monk on Mar 14, 2017 at 17:33 UTC |