vorp on 01/04/10

# Statistics

Viewed 629 times
Favorited by 0 user(s)

# Scan file for Floating point numbers with regex

/ Published in: Perl
Save to your folder(s)

Scans a file for a series of floating point numbers separated by spaces.

Copy this code and paste it in your HTML
`#!/usr/bin/perl# Usage in command line: perl <app> <file-to-be-converted> <width> <height># e.g. <app> myGeo.geo 30 30# Parse .geo file and reformat as REFRACT-style height data file my \$origfile = \$ARGV[0];my \$sizeX = \$ARGV[1];my \$sizeY = \$ARGV[2]; my \$outfile  = "refract_" . \$origfile; my %hTmp; open (IN, "<\$origfile")  or die "Couldn't open input file: \$!"; #open (OUT, ">\$outfile") or die "Couldn't open output file: \$!";  my \$numPoints = 0;my \$rowPoints = 0;my \$dataOut = ""; while (my \$sLine = <IN>) {	#print "line\n";	#if (\$sLine =~ m/^([-+]?([0-9]*\.)?[0-9]+)  ([-+]?([0-9]*\.)?[0-9]+) ([-+]?([0-9]*\.)?[0-9]+) [-+]?([0-9]*\.)?[0-9]+/) {	if (\$sLine =~ m/^([-+]?[0-9]*\.?[0-9]+)\s+([-+]?[0-9]*\.?[0-9]+)\s+([-+]?[0-9]*\.?[0-9]+)\s+([-+]?[0-9]*\.?[0-9]+)\s+\$/ )  {		my \$x = \$1;		my \$y = \$2;		my \$z = \$3;		my \$myString = sprintf("%.4f", \$y);		\$dataOut .= "\$myString ";		\$rowPoints++;		if (\$rowPoints >= \$sizeX) {			\$dataOut .= "\n";			\$rowPoints = 0;		}		\$numPoints++;	}}#close OUT;close IN;#print "Num points: \$numPoints \n";print \$dataOut;`

## Comments

Subscribe to comments

You need to login to post a comment.