Reading numbers from external text file
-
Next question.
This is the code I'm experimenting with:@settings = [] def load_file(file_path, status) @filepath = UI.openpanel("Open File", "c;/", "TXT|*.txt||") @settings = File.readlines(@filepath) return 0 # Success end load_file(@filepath, true) puts @settings[2].split(' ')[1].to_f
After loading the file I managed to get the number as a float but after running this I also get a:
3.4919929866069115 #This is a number I want to read. So this is fine. "Nil result (no result returned or run failed)" #Why do I get this?
-
You don't need to return anything from the method.
So delete the unneeded line.
return 0 # Success
You haven't posted all of your code, so it could be something elsewhere ?
A simpler version would be...
@settings = [] def load_file() filepath = UI.openpanel("Open File", "c;/", "TXT|*.txt||") if filepath ### allows a user to Cancel @settings = File.readlines(filepath) end end load_file() puts @settings[2].split(' ')[1].to_f ### to see what @settings looks like use; p @settings ### This will show spacers, \", \n etc
-
Thanks, but I still get the Nil result even after removing the "Return 0".
I even get it when running your code.For now I'm just experimenting with this part so there is no other code.
The line I'm testing with looks like this:
"x"; 3.4919929866069115,
-
Did you use the whole code with
p @settings
at the end ?
What do the first few lines of its output look like ?
Also adding:puts @settings[2].split(' ')
andputs @settings[2].split(' ')[1]
to see what's up... -
["{\n", " \"principalPoint\"; {\n", " \"x\"; 3.4919929866069115,\n", " \"y\"; -0.017301744356540385\n", " },\n", " \"viewTransform\"; {\n", " \"rows\"; [\n", " [\n", " -0.9075910551963806,\n", " 0.4198243379864999,\n", " -0.005099192261283809,\n", " -31.940051338855746\n", " ],\n", " [\n", " 0.00419330408856825,\n", " -0.003080616899800699,\n", " -0.9999864629085424,\n", " -0.10391287329462531\n", " ],\n", " [\n", " -0.4198343634438959,\n", " -0.9076001515170179,\n", " 0.0010354892297866703,\n", " -17.250974969132418\n", " ],\n", " [\n", " 0,\n", " 0,\n", " 0,\n", " 1\n", " ]\n", " ]\n", " },\n", " \"cameraTransform\"; {\n", " \"rows\"; [\n", " [\n", " -0.9075910551963807,\n", " 0.00419330408856825,\n", " -0.41983436344389585,\n", " -36.23062125433451\n", " ],\n", " [\n", " 0.41982433798650004,\n", " -0.0030806168998006997,\n", " -0.9076001515170179,\n", " -2.2480967029645083\n", " ],\n", " [\n", " -0.005099192261283809,\n", " -0.9999864629085424,\n", " 0.0010354892297866703,\n", " -0.24891673044480064\n", " ],\n", " [\n", " 0,\n", " 0,\n", " 0,\n", " 1\n", " ]\n", " ]\n", " },\n", " \"horizontalFieldOfView\"; 0.9813963064459168,\n", " \"verticalFieldOfView\"; 0.6616350540688956,\n", " \"vanishingPoints\"; [\n", " {\n", " \"x\"; -0.5541295463355106,\n", " \"y\"; 0.0013923823242965234\n", " },\n", " {\n", " \"x\"; 4.357757655415969,\n", " \"y\"; -0.02365461394665286\n", " },\n", " {\n", " \"x\"; 12.708844850226638,\n", " \"y\"; 1807.4703594352652\n", " }\n", " ],\n", " \"vanishingPointAxes\"; [\n", " \"xPositive\",\n", " \"yPositive\",\n", " \"zPositive\"\n", " ],\n", " \"relativeFocalLength\"; 1.8716593429474995,\n", " \"imageWidth\"; 5000,\n", " \"imageHeight\"; 3214\n", "}"] "[\"{\\n\", \" \\\"principalPoint\\\"; {\\n\", \" \\\"x\\\"; 3.4919929866069115,\\n\", \" \\\"y\\\"; -0.017301744356540385\\n\", \" },\\n\", \" \\\"viewTransform\\\"; {\\n\", \" \\\"rows\\\"; [\\n\", \" [\\n\", \" -0.9075910551963806,\\n\", \" 0.4198243379864999,\\n\", \" -0.005099192261283809,\\n\", \" -31.940051338855746\\n\", \" ],\\n\", \" [\\n\", \" 0.00419330408856825,\\n\", \" -0.003080616899800699,\\n\", \" -0.9999864629085424,\\n\", \" -0.10391287329462531\\n\", \" ],\\n\", \" [\\n\", \" -0.4198343634438959,\\n\", \" -0.9076001515170179,\\n\", \" 0.0010354892297866703,\\n\", \" -17.250974969132418\\n\", \" ],\\n\", \" [\\n\", \" 0,\\n\", \" 0,\\n\", \" 0,\\n\", \" 1\\n\", \" ]\\n\", \" ]\\n\", \" },\\n\", \" \\\"cameraTransform\\\"; {\\n\", \" \\\"rows\\\"; [\\n\", \" [\\n\", \" -0.9075910551963807,\\n\", \" 0.00419330408856825,\\n\", \" -0.41983436344389585,\\n\", \" -36.23062125433451\\n\", \" ],\\n\", \" [\\n\", \" 0.41982433798650004,\\n\", \" -0.0030806168998006997,\\n\", \" -0.9076001515170179,\\n\", \" -2.2480967029645083\\n\", \" ],\\n\", \" [\\n\", \" -0.005099192261283809,\\n\", \" -0.9999864629085424,\\n\", \" 0.0010354892297866703,\\n\", \" -0.24891673044480064\\n\", \" ],\\n\", \" [\\n\", \" 0,\\n\", \" 0,\\n\", \" 0,\\n\", \" 1\\n\", \" ]\\n\", \" ]\\n\", \" },\\n\", \" \\\"horizontalFieldOfView\\\"; 0.9813963064459168,\\n\", \" \\\"verticalFieldOfView\\\"; 0.6616350540688956,\\n\", \" \\\"vanishingPoints\\\"; [\\n\", \" {\\n\", \" \\\"x\\\"; -0.5541295463355106,\\n\", \" \\\"y\\\"; 0.0013923823242965234\\n\", \" },\\n\", \" {\\n\", \" \\\"x\\\"; 4.357757655415969,\\n\", \" \\\"y\\\"; -0.02365461394665286\\n\", \" },\\n\", \" {\\n\", \" \\\"x\\\"; 12.708844850226638,\\n\", \" \\\"y\\\"; 1807.4703594352652\\n\", \" }\\n\", \" ],\\n\", \" \\\"vanishingPointAxes\\\"; [\\n\", \" \\\"xPositive\\\",\\n\", \" \\\"yPositive\\\",\\n\", \" \\\"zPositive\\\"\\n\", \" ],\\n\", \" \\\"relativeFocalLength\\\"; 1.8716593429474995,\\n\", \" \\\"imageWidth\\\"; 5000,\\n\", \" \\\"imageHeight\\\"; 3214\\n\", \"}\"]"
-
3.4919929866069115 "x"; 3.4919929866069115, "Nil result (no result returned or run failed)"
-
3.4919929866069115 3.4919929866069115, "Nil result (no result returned or run failed)"
-
Here is the txt file I'm loading. (It's a json file from the beginning.)
-
Are you running this in the Ruby Console, or in some other console extension ?
Try this without the 'def
'filepath = UI.openpanel("Open File", "C;/", "TXT|*.txt||") if filepath ### allows user to Cancel settings = File.readlines(filepath) puts settings[2].split(' ')[1].to_f else puts "Canceled!" end
-
Our posts crossed.
Both my versions of the code work fine with your TXT file, with no error [in Ruby Console].
So do you use a console-app ? -
Interesting.
Running your latest code in the console works but running it in SketchUp Ruby Code Editor by Alexander Schreyer gives the Nil result also.I also tried your first version in the console and it worked without errors.
So is it safe to continue if it's just something in his Code editor app that causes the Nil result? -
Most of these console-apps are not perfect...
I only ever use the Ruby Console.
-
Thanks for all the help so far. I'm sure there will be more later on...
If only the Ruby console was multi lines. It's hard to edit in the small window. -
I always edit my code in a Notepad++/TextWrangler window [with Ruby text-highlighting], then I copy/paste it into the Ruby Console's bottom bar + enter, to run it.
The Ruby Console accepts multi-line code if pasted, but you can only manually type in one line at a time! -
I use Notepad++ as well but use the ruby code editor when testing out snippets without having to restart SketchUp.
Now I’ll continue with the main work.
Advertisement