Is an elegant way of doing this?
h.keys.sort.each { |key|
puts h[key]
}
works but seems unneccarily complicated.
Is there some analogy to the following Fortran 90 snipplet?
DIMENSION a (30, 40), i (5)
PARAMETER (i = (/5, 10, 15, 20, 25 /) )
x1 = a(1,
x2 = a(10, 2:40:2)
x3 = a(1:10, 2:40:2)
x4 = f(2, i)
! x1 through x4 are arrays
! x1 is 1st row of a
! x2 is every 2nd element of the 10th row starting with 2nd element
! x3 is every 2nd element of rows 1 through 10 " " " "
! x4 is elements 5, 10, 15, 20, 25 of 2nd row
‘h.sort[*][1].each’ would be great…
Josef ‘Jupp’ Schugt
···
–
jupp (AT) gmx (DOT) de :. …:
::…::::::
http://jupp.tux.nu ::::::::::::::::::
’’’’’’’’’’ dxm
Is an elegant way of doing this?
h.keys.sort.each { |key|
puts h[key]
}
works but seems unneccarily complicated.
I don’t think that this looks complicated, but here is an alternative:
h.sort will return an array of arrays. Like this:
h = { “name” => “Daniel”, “field” => “Math” }
=> {“name”=>“Daniel”, “field”=>“Math”}
h.sort
=> [[“field”, “Math”], [“name”, “Daniel”]]
So Hash#sort will sort by the keys and return a 2-dimmensional array.
So your snipet could be written as:
h.sort.each { |item|
puts item[0]
}
I don’t know if you think that’s any simpler.
But if all you really want is to print the key values, just do:
puts h.keys.sort
But I suspect that this is not what you really want to do and your snipet
was just an example.
···
On Tue, Mar 18, 2003 at 08:49:45AM +0900, Josef ‘Jupp’ Schugt wrote:
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
Hello Josef,
Tuesday, March 18, 2003, 2:49:45 AM, you wrote:
Is an elegant way of doing this?
h.keys.sort.each { |key|
puts h[key]
}
puts h[h.keys.sort]
works but seems unneccarily complicated.
Is there some analogy to the following Fortran 90 snipplet?
DIMENSION a (30, 40), i (5)
PARAMETER (i = (/5, 10, 15, 20, 25 /) )
x1 = a(1,
x2 = a(10, 2:40:2)
x3 = a(1:10, 2:40:2)
x4 = f(2, i)
! x1 through x4 are arrays
! x1 is 1st row of a
! x2 is every 2nd element of the 10th row starting with 2nd element
! x3 is every 2nd element of rows 1 through 10 " " " "
! x4 is elements 5, 10, 15, 20, 25 of 2nd row
‘h.sort[*][1].each’ would be great…
you also can use Range for subscripring array. For a real work
with multidimensional arrays it is better to use specific extension,
NumericalArray? (i don’t remember)
···
–
Best regards,
Bulat mailto:bulatz@integ.ru
Is an elegant way of doing this?
h.keys.sort.each { |key|
puts h[key]
}
#!/usr/bin/env ruby
require 'rbtree' # an RAA package
h = RBTree.new
h['answer'] = 42
h['hero'] = 'ford'
h.each do |k,v|
puts "#{k} -> #{v}"
end
answer → 42
hero → ford
just like a hash, but ordered.
Is there some analogy to the following Fortran 90 snipplet?
http://www.ir.isas.ac.jp/~masa/ruby/index-e.html
-a
···
On Tue, 18 Mar 2003, Josef ‘Jupp’ Schugt wrote:
–
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
Hi!
h.keys.sort.each { |key|
puts h[key]
}
Thanks for the different alternatives for that. I think that
h.sort.each { |key, value|
puts value
}
is the best version but different people prefer different coding
styles. Actually the method that did entrouble me was:
def print_sorted(hash, keys, separator)
i = 0
while i < keys.length
x = hash[keys[i]]
if x.nil?
puts “-”
else
print separator unless i == 0
print x.empty? ? ‘-’ : x.join(', ')
end
i += 1
end
end
It was used in a script that illustrates the use of Periodic.rb. the
hash contains all language versions of an elements name where. The
language versions in turn are arrays because in some languages there
is more than only one name for certain elements. The proposed
solution did help me condense the above code to:
def print_sorted(hash, separator)
print hash.sort.map { |key, value|
(value.nil? or value.empty?) ? ‘-’ : value.join(', ')
}.join(separator)
end
To me that looks more readable than the original version.
Josef ‘Jupp’ Schugt
···
Or,
h.sort.each do |key, value|
puts key
end
Gavin
···
On Tuesday, March 18, 2003, 10:59:34 AM, Daniel wrote:
So Hash#sort will sort by the keys and return a 2-dimmensional array.
So your snipet could be written as:
h.sort.each { |item|
puts item[0]
}
Hello Bulat,
Tuesday, March 18, 2003, 8:18:20 AM, you wrote:
Hello Josef,
Tuesday, March 18, 2003, 2:49:45 AM, you wrote:
Is an elegant way of doing this?
h.keys.sort.each { |key|
puts h[key]
}
puts h[h.keys.sort]
oh, sorry! this not work because hash key can be an array and
matz don’t implemented alternative method because he don’t have
a vivid name for it. so
puts h.keys.sort.map { |i| h[i] }
···
–
Best regards,
Bulat mailto:bulatz@integ.ru
puts h[h.keys.sort]
oh, sorry! this not work because hash key can be an array and
matz don’t implemented alternative method because he don’t have
a vivid name for it. so
Actually he has, and he has three.
if h.respond_to? :select # 1.7 or later
puts(*h.select(*h.keys.sort))
else
puts(*h.indices(*h.keys.sort)) or
puts(*h.indexes(*h.keys.sort))
end
FUKUMOTO Atsushi
fukumoto@imasy.or.jp
I wrote:
if h.respond_to? :select # 1.7 or later
oops, that must have been
if Hash.instance_methods.include? “select” # 1.7 or later
puts(*h.select(*h.keys.sort))
else
puts(*h.indices(*h.keys.sort)) or
puts(*h.indexes(*h.keys.sort))
end
FUKUMOTO Atsushi
fukumoto@imasy.or.jp