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

**URL:** <https://www.ruby-forum.com/t/push-and-pop-on-a-priority-queue-appears-to-change-nature-of-array/176224>\
**Category:** Ruby\
**Created:** [September 27, 2009, 7:38am UTC](https://www.ruby-forum.com/t/push-and-pop-on-a-priority-queue-appears-to-change-nature-of-array/176224 "2009-09-27T07:38:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 27, 2009, 7:38am UTC](https://www.ruby-forum.com/t/push-and-pop-on-a-priority-queue-appears-to-change-nature-of-array/176224/1 "2009-09-27T07:38:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 27, 2009, 8:39am UTC](https://www.ruby-forum.com/t/push-and-pop-on-a-priority-queue-appears-to-change-nature-of-array/176224/2 "2009-09-27T08:39:16Z")

</div>

On Sat, Sep 26, 2009 at 10:38 PM, Mason K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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 🙂

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

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 27, 2009, 10:35am UTC](https://www.ruby-forum.com/t/push-and-pop-on-a-priority-queue-appears-to-change-nature-of-array/176224/3 "2009-09-27T10:35:11Z")

</div>

On Sun, Sep 27, 2009 at 1:36 AM, John W Higgins [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Sat, Sep 26, 2009 at 10:38 PM, Mason K. \<removed\_email\_address@domain.invalid
> 
> > 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 🙂

Sadly, If I had put more emphasis on the very last sentence of  
[ActiveState Community - Boosting coder and team productivity with ready-to-use open source languages and tools.](http://aspn.activestate.com/ASPN/Mail/Message/ruby-talk/3754953) , might  
have  
saved you the trouble.

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 27, 2009, 5:39pm UTC](https://www.ruby-forum.com/t/push-and-pop-on-a-priority-queue-appears-to-change-nature-of-array/176224/4 "2009-09-27T17:39:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 27, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/push-and-pop-on-a-priority-queue-appears-to-change-nature-of-array/176224/5 "2009-09-27T17:42:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![masonkessinger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4da419/32.png) [@masonkessinger](https://www.ruby-forum.com/u/masonkessinger)\
**Post date:** [September 27, 2009, 9:59pm UTC](https://www.ruby-forum.com/t/push-and-pop-on-a-priority-queue-appears-to-change-nature-of-array/176224/6 "2009-09-27T21:59:25Z")

</div>

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
