PDA

View Full Version : csv and while loop



ryaden
06-05-2018, 12:50 AM
I have been struggling with a script freezing and don't know why.
I have been experimenting with csv files to store information - like scrolls I have saved and gems I am hoarding.

My script has been slowing when I get to this loop, essentially no game data goes through once I do this but I can enter commands but can't see what happens.

while line = get
if line =~ /ACCEPT/
fput "accept"
fput "wear pouch"
break
end
end

I am not concerned there is a problem with the loop, because when copied and pasted into a test script in isolation and it works fine. It is freezing this script in the game. If I kill the script the game goes back to working normally.

I think this problem may be caused by my use of a csv file. I have a few lines in the script that may be causing problems and wonder if anyone has any idea how I might fix this.

1.
require 'csv'
get = CSV.read('hoardgem.csv') #this is a file of #_of_gems and gem_name

2. also I have created a string to use in a regular expression (I need these to be available as an array but also as a string for regular expression search).

get_gems = ["gem_name", "another_gem_name", "ect"]

@gems_to_hoard = ""
get_gems.each do |obj|
if @gems_to_hoard.empty?
@gems_to_hoard = obj
else
@gems_to_hoard = @gems_to_hoard + "|" + obj
end
end

#this should yield "gem name|another gem name|ect"

As I say all the other code seems to work fine. It is simply the while line = get part that freezes. I appreciate any thoughts.

Ryaden

horibu
06-05-2018, 01:35 AM
Try using a different variable name than get for your CSV file read. get is a function within lich so perhaps it's doing something funky there.

Besides that I'm not sure what to tell you without seeing the actual code. I'm also typing this while trying to fall asleep, so you have that working against me too.

Imperarx
06-05-2018, 10:29 AM
Choose another variable name is right, since you're actually masking the get method that you want to use later on. On a different note, what you're doing in section two can be replaced with

@gems_to_hoard = get_gems.join("|")
No need to build it concatenate by concatenate yourself.

ryaden
06-05-2018, 10:22 PM
Thanks so much. It turns out the get = was the problem in the script. Still learning these small details that can cause problems. I appreciate the thought on .join as well. Just started learning less than a year ago, so I enjoy learning more efficient methods.