PDA

View Full Version : Why wont this work!?



pabstblueribbon
06-17-2009, 09:51 AM
Okay.

Preface: I know Gib has already done a box dropper in a similar thread. I stole some of the logic from it and I know it does the job. However I'm trying to learn how .scan and other things work so, here I am, trying to hack some of the logic from Gib's script and another script so it actually scans the contents of a container and empties anything in the container that is defined in an array.

The problem: For some reason it works awesome, up until there is only 1 of the items in my item array and then it wont take out the last item. Pointers as to why, how else to accomplish this, or if someone could actually explain whats going on I would be extremely grateful. (Learning to scan a containers contents would be really useful)

The definition:


def boxdrop

fput "drop right" if checkright =~ /(?:strong)?box|coffer|chest|trunk/
fput "stow right" if checkright
fput "drop left" if checkleft =~ /(?:strong)?box|coffer|chest|trunk/
fput "stow left" if checkleft
boxdb = ["coffer", "chest", "strongbox", "trunk", "box"]
fput "turn #{checkname} disk"

$boxcont.split(',').each { |boxcont|
fput("look in my #{boxcont}").scan(/\b(?:#{boxdb.join('|')})(?=,|\.)/).each { |box|
fput "take my #{box} from my #{boxcont}"
fput "drop my #{box}"
sleep 0.15
fput "drop my #{box}" if(checkright(box) or checkleft(box))
}
}
end

pabstblueribbon
06-17-2009, 11:13 AM
Got it to work. I think.

Shaelun
07-10-2009, 03:16 PM
Since you asked, check out http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.scan for details on how scan works.

I'm not sure if it's still available with Tillmen's version, but it probably is -- String#split_as_list is what I always used in my own scripts, if memory serves. The implementation from my "lich-lib.rb" file, if you're curious. All the other methods used are standard Ruby fare, you can find them in the documentation I linked above:



class String
def split_as_list
string = self.sub(/^You (?:also see|notice) |^In the .+ you see /, ',')
string.sub('.','').sub(/ and (an?|some|the)/, ', \1').split(',').reject { |str|
str.strip.empty?
}.collect { |str| str.lstrip }
end
end


In a nutshell, it first replaces the "In the thingie you see" part with a comma. Then it snips out the period. That leaves something like ",a coin, a rock and a stick" as the string. Then it replaces the "and a" part of the string with another comma, leaving ",a coin, a rock, a stick" as the string. Then it splits the string up into an array of [ "", "a coin", "a rock", "a stick" ]. Then it rejects any elements that are empty strings (the first one is an empty string, because we've placed "In the thingie you see" with a comma -- fuck if I know why, but that's how I decided to do it all that time ago it seems.) Then it collects all remaining values after it calls String#lstrip on them (stands for "left strip" -- strips any whitespace off the left side of the string.) Then, since Ruby is Ruby, the result of that last collect method gets returned as the result of someString.split_as_list.

Hope that helps.