Push and Pop on a Priority Queue appears to change nature of Array

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

I defined a Priority Queue to create an ascending queue with:
open_queue = PQueue.new(proc{|x,y| x[0][0]<y[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]])

but when later on I pop it out of the open_queue
removed_entry = []
removed_entry = open_queue.pop
and then move it back to current_state
current_state = removed_entry[3].dup <== I’ve tried this
with
and without the dup
current_state appears to be an array with one entry of “316520478”
puts "Current_state = " + current_state.to_s
==>
316520478 so clearly this is the value it has
puts "Class of Current_state = " + current_state.class.to_s ==>
Array so clearly it is still an array
If I execute the index function again it gives a nil value, as you would
expect with one 9 digit number in the array
I’ve tried with and without the quotes:
puts "Current_state.index(0) = " + current_state.index(“0”).to_s
and
puts "Current_state.index(0) = " + current_state.index(0).to_s

and
puts "Length of current_state array = " + current_state.length.to_s
displays a value of 1, confirming that it has become a single nine digit
number. If the updated array has only one entry in it, of course it
would
not be a 0 or “0” or any value but even “316520478” returns nothing, not
a
zero, which is strange.

I need to have the current_state array stay as an array of 9 single
digit
numbers.

What am I ignoring? How am I changing the nature of the current_state
array
when I push or pop it into or from the queue? I need to have an array
with
9 separate digits in it so I can index them.

Help!

No Sam

On Sat, Sep 26, 2009 at 10:38 PM, Mason K.
[email protected]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]<y[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 :slight_smile:

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

On Sun, Sep 27, 2009 at 1:36 AM, John W Higgins [email protected]
wrote:

On Sat, Sep 26, 2009 at 10:38 PM, Mason K. <[email protected]

wrote:
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

I enjoyed your post :slight_smile:

Sadly, If I had put more emphasis on the very last sentence of
http://aspn.activestate.com/ASPN/Mail/Message/ruby-talk/3754953 , might
have
saved you the trouble.

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

Thanks, Josh. This is a learning experience for me. And the things I
learn
the hard way are the ones that stick best.

No Sam

Josh,

This also means that I have to change the way I construct the sorting
mechanism for the Priority Queue. So instead of
pq=PQueue.new(proc{|x,y| x[0][0<y[0][0]]})
pq.push([[12], [“n1”], [123456780], [123456708]])
I have to use:
pq=PQueue.new(proc{|x,y| x[0]>y[0]})
pq.push([12, “n1”, 123456780, 123456708])

Also I found that I had the “<” wrong. Should be “>” for an ascending
sort.

Thanks again for your help. I’m making progress. But, geez, is it
slow!

No Sam