in reply to Re: accessing specific data in a file
in thread accessing specific data in a file
not the best code but it works like I said.#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; ########the real testing begins here################### my (@p, $file, $output_file, $data, $OUTPUT, $key, $points, $i); ###############open file for reading $file = <~/thesis/handmodel/testtext.txt>; open(DATA, "<$file"); $data = join '', <DATA>; close(DATA); ############### parse input my @points; while ($data =~ /.*?(Coordinate).*?point \[(.*?)\]/gs){ $key=$1; $points=$2; print "key is $key\n"; @p = $points =~ /(-?\d+\.\d+)/g; #print "@p"; #print "$p[1]\n"; #my @points; for ($i=0; $i < $#p; $i += 3){ push @points, [$p[$i],$p[$i+1],$p[$i+2], 1]; } #print {$OUTPUT} Dumper(\@points); print "$#points\n"; #@points = (); #clear variable } ################open file for output $output_file = <~/thesis/handmodel/testpoints.cpp>; open($OUTPUT, ">$output_file") or die "Can't open $output_file for wri +ting: $!"; print {$OUTPUT} "\/\/Author: Phoenix\n"; print {$OUTPUT} "\/\/Description : Homogeneous coordinate values for h +and model\n"; print {$OUTPUT} "\/\/\t $output_file 31 March 2006 \n\n"; print {$OUTPUT} "#ifndef POINTS_HPP\n"; print {$OUTPUT} "#define POINTS_HPP\n\n"; print {$OUTPUT} "const double points\[$#points\]\[4\] = \{\n"; for my $j ( 0 .. $#points){ my $k=0; print {$OUTPUT}"\t\{$points[$j][$k],$points[$j][$k+1], $points[$j] +[$k+2], 1\}" ; if ($j != $#points){ print {$OUTPUT} ",\n"; } else { print{$OUTPUT} "\n\};\n\n#endif"; } } close($OUTPUT);
I wanted to extract Coordinate points, normal points and the coordIndex. to understand how those are specified, if you hae the interest, readgeometry IndexedFaceSet { coord Coordinate { point [ 265.185 -166.225 -510.375, 264.529 -166.901 -513.43, 269.321 -166.425 -510.918, 271.021 -166.956 -513.279, 271.223 -167.21 -514.637, 272.77 -166.984 -514.019, 270.767 -167.555 -516.859, 268.668 -167.344 -515.143, 272.884 -167.285 -515.945, 266.267 -167.539 -516.835, 267.193 -167.766 -518.544, 272.686 -167.438 -517.418, 269.214 -167.83 -519.399, 274.761 -166.996 -515.928, 275.801 -166.946 -519.035, 264.372 -167.451 -517.076, 266.21 -167.785 -519.148, 263.801 -167.367 -516.587, 269.266 -167.919 -521.463, 271.322 -167.821 -522.477, 266.656 -168.197 -528.597, 269.644 -168.342 -527.906, 267.007 -167.981 -524.961, 264.07 -167.493 -517.639, 263.244 -167.65 -520.707, 267.726 -167.91 -521.09, 264.468 -167.739 -523.493] } colorPerVertex FALSE normal Normal { vector [ 0.283327 0.589642 -0.756338, 0.290016 0.584234 -0.757999, 0.290674 0.591917 -0.75176, 0.294405 0.586667 -0.754418, 0.277385 0.586313 -0.761114, 0.281287 0.573944 -0.769069, 0.280843 0.489526 -0.825525, 0.292972 0.558267 -0.776212, 0.28304 0.543153 -0.790489, 0.31842 0.562395 -0.763099, 0.418496 0.521052 -0.743885, 0.438261 0.572811 -0.692687, 0.0414296 0.210334 -0.976751, 0.144261 0.318093 -0.937019, -0.169459 0.196165 -0.965817, 0.284861 0.187786 -0.939995, 0.280689 0.367447 -0.886677, 0.228712 0.200214 -0.952683, 0.235995 0.0929388 -0.9673, 0.240012 0.152475 -0.958721, 0.252035 0.319933 -0.913302, 0.346045 0.0724201 -0.935419, 0.342717 0.0627962 -0.937338, 0.331245 0.114564 -0.936564, 0.283689 0.259392 -0.923167, 0.383508 0.0867687 -0.919453, 0.200092 0.0675543 -0.977445, 0.215463 0.198183 -0.95619, 0.213162 0.0833883 -0.973452, 0.258661 0.478244 -0.839272, 0.350493 0.430564 -0.831726, 0.28955 0.31473 -0.903939, 0.370349 0.251505 -0.894196, 0.267697 0.593232 -0.759219, 0.269102 0.564528 -0.780315, 0.512169 0.531879 -0.674379 ] } coordIndex [0, 1, 2, -1, 2, 1, 3, -1, 4, 5, 1, -1, 6, 7, 8, -1, 9, 10, 11, -1, 12, 13, 14, -1, 15, 16, 17, -1, 18, 19, 15, -1, 16, 20, 17, -1, 21, 22, 23, -1, 23, 24, 19, -1, 25, 23, 22, -1, 26, 27, 12, -1, 28, 17, 27, -1, 8, 5, 29, -1, 13, 27, 20, -1, 27, 17, 20, -1, 8, 16, 6, -1, 6, 30, 9, -1, 16, 15, 31, -1, 19, 24, 15, -1, 10, 9, 30, -1, 30, 6, 24, -1, 24, 23, 32, -1, 8, 3, 1, -1, 4, 33, 5, -1, 7, 6, 34, -1, 1, 5, 8, -1, 35, 11, 10, -1, 12, 27, 13, -1, 32, 23, 25, -1, 34, 6, 9, -1, 29, 13, 20, -1, 29, 20, 16, -1, 29, 16, 8, -1, 16, 31, 6, -1, 31, 15, 24, -1, 24, 6, 31, -1, 30, 24, 32, -1, 30, 32, 10, -1 ] } }
|
---|