From: Mason Kelsey Date: 2009-09-28T00:38:55+09:00 Subject: Re: Push and Pop on a Priority Queue appears to change nature of Array --0016364ed9d4662f62047490faea Content-Type: text/plain; charset=ISO-8859-1 Thank you much. Sorry about the wordiness but I was not sure where the source of the problem was and didn't want to leave out what might have been the cause. The brackets were the last thing I would have suspected. So also thank you for the instruction in Ruby. I've have tried your suggestion and removed the second sets of brackets, and it works now. I should point out that the command p removed_entry[3] did not put brackets around the value in the display. So I would not have known what the problem was using the p command. I had not tried the p removed_entry[3][0] display. Again, your helpfulness is greatly appreciated. Working with multi-dimensional arrays is tricky in Ruby for programmers new to the language. Now I can move beyond that logjam. Ruby program to solve 8-puzzle is 90% completed. I'm sure that many parts could be more efficiently written if I understood some of the finer capabilities of Ruby. And I eventually will. I'm persistent. No Sam On Sun, Sep 27, 2009 at 2:36 AM, John W Higgins wrote: > On Sat, Sep 26, 2009 at 10:38 PM, Mason Kelsey >wrote: > > > I'm stumped. Worked on this problem for over three hours. I created an > > array: > > current_state = [3, 1, 6, 5, 2, 0, 4, 7, 8] > > and > > puts "Current_state.index(0) = " + current_state.index(0).to_s displays > > a 5, as would be expected > > puts "Length of current_state array = " + current_state.length.to_s > > displays a 9, as expected > > > > > First, not trying to be an idiot or anything but you are using WAY too many > words mixed in with your code here to describe your issue. Keeping things > simple but descriptive will get you much more assistance because it's > easier > to read. > > Lets take the 3 lines above. This would be much simpler > > current_state = [3, 1, 6, 5, 2, 0, 4, 7, 8] > p current_state #=> [3, 1, 6, 5, 2, 0, 4, 7, 8] > > note the #=>, this is the nicer way of describing what you get back from a > line - keeps it simple and less "wordy". > > Also, using p or even better doing a require 'pp' and using pp gives you > better information when debugging because it will return more "object" > information as you'll see below. > > > > > I defined a Priority Queue to create an ascending queue with: > > open_queue = PQueue.new(proc{|x,y| x[0][0] > > > Then I push it into the Priority Queue with four other objects: > > open_queue.push([[hv], [node_level], [direction_of_prior_move], > > [current_state], [parent_node]]) > > > > > If you can make your problem simpler - please try to do so. If the priority > queue is causing you a problem then try your work just adding your single > array into the queue - again it's simpler and is easier for folks to > duplicate. We have no idea what the other elements might contain, nor does > it really matter. > > Now that I've been branded the village idiot, let's solve your problem. > It's > actually simple, you've added arrays of arrays to your queue as opposed to > an array of your elements. The inner []'s are not necessary and are what is > messing you up. > > open_queue.push([hv, node_level, direction_of_prior_move, current_state, > parent_node]) # correct call > > As to why you would have been better with p instead of puts - well if you > did the following the error would have been much easier to see (assuming > the > use of your original code). > > removed_entry = open_queue.pop > > p removed_entry[3] #=>[[3, 1, 6, 5, 2, 0, 4, 7, 8]] > p current_state #=> [3, 1, 6, 5, 2, 0, 4, 7, 8] > > current_state was the original entry you pushed in from above - note the > lack of the second [] around the original array. That may not have given > you > the immediate answer - but if you had given that as your problem "why are > there an extra set of brackets around my value" or "why is my value wrapped > in an array" - it would have made the question much easier :) > > One other small last tip - when you're debugging - don't be "cute" and try > to dig deeper then you need to start the analysis - don't worry about what > the index of 0 is inside your array - examine the array itself. For > example, > again assuming your original code the following might have been much more > help to you. > > p removed_entry[3] #=>[[3, 1, 6, 5, 2, 0, 4, 7, 8]] > p removed_entry.class #>Array > p removed_entry[3].length #=>1 ???huh??? > p removed_entry[3][0] #=>[3, 1, 6, 5, 2, 0, 4, 7, 8] ???huh?? > p removed_entry[3][0].class #>Array ???huh??? > > Certainly if that didn't start leading you to examine the extra []'s - > again > it would have made your question much simpler to look at and diagnose. > > So now that I've spent wayyyyy too many words myself - the bottom line is > that you don't want to wrap each element inside an array - get rid of the > []'s around each element and you'll be in business. > > John > --0016364ed9d4662f62047490faea--