Load text file and assign to variables
-
hi,
when i reopen my text-file with parameters (1 parameter per line)by
count = 0
File.open('C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt', 'r') do |f1|
while line = f1.gets
puts lineend
end
i can see the values listed in the console // OK
BUT
when i try
count = 0
File.open('C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt', 'r') do |f1|
while line = f1.gets
puts linevalue[count]=f1.gets count = count + 1 end
end
it does not work
ma question is:
how can i assign the result of f1.gets to a row of variables in this loop?
thanx you
stan
-
I think perhaps an easier format to think about would be:
file = 'C;/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt' IO.readlines(file).each_with_index{|line, i| line.chomp! ### removes the '\n' from the end of the line next if line.empty? ### this traps for empty lines next if line =~ /^[#]/ ### lines starting with e.g. '#' are ignored; allows for headers/comments/disabling puts i puts line ### or more likely do your stuff using the string 'line' }
Note: if you don't need the index 'i', then use '
).each{|line|
' instead... -
hi tig and hello again,
this is great and worked inmedialtely in the ruby console.
so:
i counts the lines
line gets the value per linemaybe a small tip (i still think too much in macro-basic)
when i retrieve those values form the file,
this syntax makes problems:assign variables from file (like reading an array from a loop)
a = line[1]
b = line[2]
c = line[3]how could i build an array within the import routine? i tried so many ways and can#t get it...
thanx stan -
file = 'C;/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt' lines=[] IO.readlines(file).each{|line| line.chomp! ### removes the '\n' from the end of the line next if line.empty? ### this traps for empty lines lines << line }
NOT sure why you need separate variables... this makes an array of the lines called 'lines'
Nowlines[0]
is the first line,lines[1]
is the second line etc [note how an array index starts at 0 not 1 !]
To find the last line uselines[-1]
etc...
So you have reference to each lines using this method, without extra variables...
If you really want to set up a series of variables then you can do it thus...### you need to set up instance variables, @v_a etc, so they get referenced outside of the {} block, plain v_a won't be referenced outside and would therefore be inaccessible... v='@v_a' lines.each{|e| eval("#{v}='#{e}'") v.next! }
Now
@v_a
will returnlines[0]
,@v_b
giveslines[1]
etc... BUT as I said sticking withlines[0]
,lines[1]
etc seems far simpler and easier to use/control... -
hi tig,
thanx a lot, i inserted the new import routine and tried different variantion.
ruby gives back:
load '01.rb'
true
Error: #<NameError: undefined local variable or methodlines' for #<Object:0x3d31654 @v_b="90", @v_a="300", @v_c="100">> C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96:in
draw_stairs'
C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:48
C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96:incall' C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96 load '01.rb' true Error: #<NoMethodError: undefined method
/' for "300":String>
C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:173:indraw_stairs' C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:48 C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96:in
call'
C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/01.rb:96so the situation is:
i am created an object in skp, which can be controlled in diverse dimension by an input menue (parameters)
when the object is created, i save the parameters in a text file (parttemp2.txt):
content of parttemp2.txt now for testing:
300
90
100it seems, that reloading these numbers gives back STRINGS and not numbers
as <ou also said, i would like to handle these imported parameters by
parameter01 = lines[0]
parameter02 = lines[1]and so on.
but it simply does not work now for me. have to study more about formats (you know, i am on ruby since few days)
this is the whole pert of the code: (where WIDTH shall become the value of line 0 in the text-file)
require 'sketchup.rb'
#require 'offset.rb'
#require 'makesoftsmooth.rb'
.
Sketchup.send_action "showRubyPanel:"#Add a menu item to launch our plugin.
UI.menu("PlugIns").add_item("01-ZF-Stairs"){
UI.messagebox("Outdoor-Stairs - Script - 131011-V 0.1j - Zdenek Fajfrlik")#result = UI.messagebox "New Definition?", MB_YESNO
#if result == 6 # YesUI.messagebox("New Definition")
end
#count = 0
#File.open('C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp.txt', 'r') do |f1|
#while line = f1.gets
#puts line#width = f1.gets #end
#end
file = 'C:/Program Files (x86)/Google/Google SketchUp 8/Plugins/zf/partemp2.txt'
lines=[]
IO.readlines(file).each{|line|
line.chomp! ### removes the '\n' from the end of the line
next if line.empty? ### this traps for empty lines
lines << line
}you need to set up instance variables, @v_a etc, so they get referenced outside of the {} block, plain v_a won't be referenced outside and would therefore be inaccessible...
v='@v_a'
lines.each{|e|
eval("#{v}='#{e}'")
v.next!
}Call
draw_object
}def draw_object
#***************************************************************************
#delete previous modelmodel = Sketchup.active_model
entities = model.active_entities
entities.to_a.each { | entity| entity.erase! }#***************************************************************************
set start default values for MENUE 1-4
width = @v_a lwidthplus = @v_b rwidthplus = @v_c #width = 500 #lwidthplus = 10 #rwidthplus = 10 stairs = 10 run = 30 rise = 13 thickness = 3 fill = 4 overlap = 6 il = 6 rc = 3 re = 0 stepdevidex = 1 sl = 100 sr = 100 mr = 80 pmod = 3 pradius = 15 thradius = 20 mhradius = 4 lines = 4 ro = 4 rol = 5 ror = 5 wangenbreitel = 30 wangenhoehel = 10 wangeslopel = 45.0 rampoffsetl = 50 rampoffsetly = 30 rampoffsetlz = 99.0 wangenbreiter = 30 wangenhoeher = 10 wangesloper = 45.0 rampoffsetr = 50 rampoffsetry = 30 rampoffsetrz = 99.0
#***************************************************************************
thank you very very much for your time and helping!
STAN -
Please use the 'code' BBS tags for large blocks of code - it's very hard to read otherwise.
You do NOT need to assign a whole load of variables with
eval
...
Just use 'lines
' OR whatever you call it as the array of values...
To find the first one uselines[0]
and so on...You cannot pass variables across methods unless you pass them as arguments after the method [def] name OR the methods are in the same module/class and are instance ones - starting with @....
You are making this far more convoluted than it needs to be...
Can we go back a few steps...
What is it you are trying to do exactly - I get part of it so far?
What is in the file you are reading in exactly [data/units/etc]?You are reading in
strings
, NOT numbers, so convert it to a float using.to_f
Then you can use/
etc on them...
parameter01 = lines[0].to_f
BUT if you know they are in cm or mm you need to use say:
parameter01 = lines[0].to_f.mm
etc...You are getting in a muddle...
-
hi tig,
that's it .
my logic was ok, your tip for the conversion of string to float was the point.basicly it works like this:
i have menues for parameters
then the geometry is created
before the end of the ruby i save all parameters to a text file (by the way still looking for the syntax to save more of them line by line)
then the user can recall the ruby and select
-my last parameters
-defaultsand so on and so on
again another step!!!!
regards stan ( i'll report , when i get further)
Advertisement