Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Storing Array in Hash

Reply
Thread Tools

Storing Array in Hash

 
 
John Bentley
Guest
Posts: n/a
 
      11-23-2008
I am having the most insane time with Ruby's Hash class.

I have Array's of varied length, but whenever they are stored and then
retrieved from a Hash they become length of 1 and all the values are
somehow combined in that first cell.

Code:

aRay = ["a","b","c"]
aRay2 = ["1","2","3","4","5"]

# prints out 3
puts aRay.size
# prints out 5
puts aRay2.size

# store them
h = {"letters" => aRay, "numbers" => aRay2}

# prints out abc
puts h.values_at("letters").to_s
# prints out 12345
puts h.values_at("numbers").to_s

# retrieve the arrays
ltrArray = h.values_at("letters")
numArray = h.values_at("numbers")

# prints out abc
puts ltrArray.to_s
# prints out 12345
puts numArray.to_s

# prints out 1
puts ltrArray.size
# prints out 1
puts numArray.size




What am I doing wrong?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Luc Heinrich
Guest
Posts: n/a
 
      11-23-2008
On 23 nov. 08, at 15:13, John Bentley wrote:

> puts h.values_at("letters").to_s


Hash#values_at returns an array of the requested values, so in your
case you get an array of array(s).

What you want is:
ltrArray = h.values_at("letters").first
numArray = h.values_at("numbers").first

--
Luc Heinrich -


 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      11-23-2008
Alle Sunday 23 November 2008, John Bentley ha scritto:
> I am having the most insane time with Ruby's Hash class.
>
> I have Array's of varied length, but whenever they are stored and then
> retrieved from a Hash they become length of 1 and all the values are
> somehow combined in that first cell.
>
> Code:
>
> aRay = ["a","b","c"]
> aRay2 = ["1","2","3","4","5"]
>
> # prints out 3
> puts aRay.size
> # prints out 5
> puts aRay2.size
>
> # store them
> h = {"letters" => aRay, "numbers" => aRay2}
>
> # prints out abc
> puts h.values_at("letters").to_s
> # prints out 12345
> puts h.values_at("numbers").to_s
>
> # retrieve the arrays
> ltrArray = h.values_at("letters")
> numArray = h.values_at("numbers")
>
> # prints out abc
> puts ltrArray.to_s
> # prints out 12345
> puts numArray.to_s
>
> # prints out 1
> puts ltrArray.size
> # prints out 1
> puts numArray.size
>
>
>
>
> What am I doing wrong?


Try replacing lines like

puts ltrArray.to_s

with

p ltrArray

and you'll understand what's happening.

By the way, you don't need to add the .to_s to the argument of a call to puts
(or p), as puts will do that for you. Also, to display the contents of hashes
and arrays, the inspect (and the associated p) method may be more useful than
the to_s (and associated puts) method.

Stefano

 
Reply With Quote
 
John Bentley
Guest
Posts: n/a
 
      11-23-2008
Luc Heinrich wrote:
> What you want is:
> ltrArray = h.values_at("letters").first
> numArray = h.values_at("numbers").first


Oh wow...I've been plugging away at this for 4 hours now and that just
fixed my problem. Thank you so much!

I'm in the process of learning Ruby through various online mediums so I
tend to miss little intricacies like that.

Thanks again.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-23-2008
On 23.11.2008 15:24, John Bentley wrote:
> Luc Heinrich wrote:
>> What you want is:
>> ltrArray = h.values_at("letters").first
>> numArray = h.values_at("numbers").first

>
> Oh wow...I've been plugging away at this for 4 hours now and that just
> fixed my problem. Thank you so much!
>
> I'm in the process of learning Ruby through various online mediums so I
> tend to miss little intricacies like that.


A good tool to try these things out is irb which comes with your Ruby
distribution. Since irb by default uses #inspect (the same that method
"p" uses) to print out objects you can immediately see what's going on.

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
storing a hash of a hash in a DBM database Colvin Perl Misc 3 12-30-2003 04:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57