=====Parsing Data Strings=====
Sometimes we have a text string consisting of numerical values separated by spaces. For example, we may have a string containing the values of the XYZ-coordinates of a body in space.
**The goal is to parse the data string and extract those three numerical values and put them into their respective working variables (x,y,z) for subsequent use.**\\
EXAMPLE:
$xyz = '+9.341607 -2.608450 +12.374303';
Above is a data string containing the coordinates (x,y,z) as a string of three numerical values separated by spaces.\\
There is a possibility, if only by accident, for multiple spaces to occur between text elements. To account for any accidental non-uniform spacing between the elements, we can create a special filter to strip any leading and trailing spaces and reduce any multiple spaces between the elements to single spaces instead, prior to splitting up the string, as in the code below. This will correctly extract the coordinates into their respective variables regardless of non-uniform spacing within the string.\\
Given the text data string: (extra spaces added for demo purposes) \\
$xyz = '+9.341607 -2.608450 +12.374303';
The splitting function will not correctly process the string if there is more than one space between any of the data elements. To remedy this, we first have to normalize the spacing between the data elements before splitting them apart by removing all white space from within the string. The following code will remove all extra white spaces from within ($xyz)\\
$xyx = PReg_Replace("/\s+/", " ", trim($xyz));
Before filtering - Variable spacing:
$xyz = '+9.341607 -2.608450 +12.374303'
After filtering - Uniform single spacing:
$xyz = '+9.341607 -2.608450 +12.374303'
-----
Here is a working example of extracting the XYZ-coordinate values from a string:
x y z = '$x $y $z'
x = $x
y = $y
z = $z
";
?>
The printed result should be:
xyz = '9.341607 -2.608458 12.374303'
x = +9.341607
y = -2.608458
z = +12.374303
Note that the xyz string now has normalized (single) spacing between the (x,y,z) values.
-----
The same technique can be applied to any data string, such as the following example for extracting RA, declination, distance and visual magnitude.
Here is another working example.
AsteroidData = '$AsteroidData'
RA = $RA
decl = $decl
dist = $dist
VMag = $VMag
";
?>
The printed resulted should be:
AsteroidData = '13.496959713 +11.608450138 7.374303475 +7.680'
RA = 13.496959713
decl = +11.608450138
dist = 7.374303475
VMag = +7.680