关联数组(或哈希)php 到 ruby

发布于 2024-11-03 01:00:35 字数 1474 浏览 5 评论 0原文

我对 ruby​​ 比较陌生,而且我来自 php。我似乎不明白的一件事是哈希和数组之间的区别。 如何在 ruby​​ 中实现关联数组? 例如。

$albums = array();
$songs = array();
$songs[] = array('title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 2', 'artist' => 'Artist 2', 'album' => 'Album 2');
$songs[] = array('title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1');
foreach($songs as $song)
{
  if( !isset($albums[$song['album']]) ) $albums[$song['album']] = array();
  $albums[$song['album']]['title'] = $song['album'];
  $albums[$song['album']]['songs'] = $song;
}
print_r($albums);
['Album 1']
  => 'title' => 'Album 1'
  => 'songs'
    => 'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'
    => 'title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1'
    => 'title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1'
    => 'title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1'
['Album 2']
  => 'title' => 'Album 2'
  => 'songs'
    => 'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'

或者甚至,我可以声明一个包含标题、艺术家和歌曲数量的数组专辑信息,而不是标题和歌曲。

我怎样才能用红宝石做类似的事情?我会用什么来实现这个目标?

I'm relatively new to ruby and i came from php. One thing that it seems i would not understand is the difference between hashes and arrays.
How do you implement associative arrays in ruby?
Eg.

$albums = array();
$songs = array();
$songs[] = array('title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 2', 'artist' => 'Artist 2', 'album' => 'Album 2');
$songs[] = array('title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1');
$songs[] = array('title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1');
foreach($songs as $song)
{
  if( !isset($albums[$song['album']]) ) $albums[$song['album']] = array();
  $albums[$song['album']]['title'] = $song['album'];
  $albums[$song['album']]['songs'] = $song;
}
print_r($albums);
['Album 1']
  => 'title' => 'Album 1'
  => 'songs'
    => 'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'
    => 'title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1'
    => 'title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1'
    => 'title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1'
['Album 2']
  => 'title' => 'Album 2'
  => 'songs'
    => 'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'

Or even, instead of title and songs i can declare an array album info with title, artist and how many songs.

How can i do something similar with ruby? what i would use to accomplish that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦归所梦 2024-11-10 01:00:35

从 PHP 或多或少直接重写:

albums = {}
songs = [
  { :title => 'Title 1', :artist => 'Artist 1', :album => 'Album 1' },
  { :title => 'Title 2', :artist => 'Artist 2', :album => 'Album 2' },
  { :title => 'Title 3', :artist => 'Artist 1', :album => 'Album 1' },
  { :title => 'Title 4', :artist => 'Artist 2', :album => 'Album 1' },
  { :title => 'Title 5', :artist => 'Artist 3', :album => 'Album 1' }
]
songs.each do |song|
  album = albums[song[:album]] ||= {}
  album[:title] = song[:title]
  (album[:songs] ||= []).push(song)
end
puts albums.inspect

更简单的是单行:

albums = songs.group_by { |song| song[:album] }

数组只有整数键,并将所有键分配给最高的键(因此,如果您分配 foo[100] = 1 ,键 0-99 的位置也会占用内存(带有 nil 值);您用方括号记下数组值:[1, 2, 3] 是一个 3 元素数组,[] 为空,

哈希值括在大括号 {} 中,并且可以包含几乎任何内容作为键,但是块可以。也可以用大括号书写,因此请注意不要混淆两者。

A more-or-less direct rewrite from your PHP:

albums = {}
songs = [
  { :title => 'Title 1', :artist => 'Artist 1', :album => 'Album 1' },
  { :title => 'Title 2', :artist => 'Artist 2', :album => 'Album 2' },
  { :title => 'Title 3', :artist => 'Artist 1', :album => 'Album 1' },
  { :title => 'Title 4', :artist => 'Artist 2', :album => 'Album 1' },
  { :title => 'Title 5', :artist => 'Artist 3', :album => 'Album 1' }
]
songs.each do |song|
  album = albums[song[:album]] ||= {}
  album[:title] = song[:title]
  (album[:songs] ||= []).push(song)
end
puts albums.inspect

Easier would be the one-liner:

albums = songs.group_by { |song| song[:album] }

Arrays have only integer keys, and allocate all of them up to the highest one (so if you assign foo[100] = 1, there will be locations for keys 0-99 also occupying memory (with nil value); you note array values with square brackets: [1, 2, 3] is a 3-element array, [] is empty.

Hashes are enclosed in curly braces {}, and can have pretty much anything as keys. However, blocks can also be written with braces, so take care not to confuse the two.

尸血腥色 2024-11-10 01:00:35

您应该能够像使用 PHP 关联数组一样使用 Ruby 哈希。

You should be able to use a Ruby Hash exactly like a PHP associative array.

橘寄 2024-11-10 01:00:35

正如您在问题标题中提到的,关联数组在 Ruby 中通常称为哈希。语法与数组略有不同。

# Normal array
normArray = ["SomeSong", "SomeGuy"]
anotherNormArray = Array.new

# Literal hash
literalHash = { "Title" => "SomeSong", "Artist" => "SomeGuy" }

# Declared beforehand
declaredHash = Hash.new
declaredHash["Songs"] = literalHash

As you mentioned in the title of your question, associative arrays are usually referred to as hashes in Ruby. The syntax is slightly different from arrays.

# Normal array
normArray = ["SomeSong", "SomeGuy"]
anotherNormArray = Array.new

# Literal hash
literalHash = { "Title" => "SomeSong", "Artist" => "SomeGuy" }

# Declared beforehand
declaredHash = Hash.new
declaredHash["Songs"] = literalHash
穿越时光隧道 2024-11-10 01:00:35

你可以在一行中完成它

songs = [
  {'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'},
  {'title' => 'Title 2', 'artist' => 'Artist 2', 'album' => 'Album 2'},
  {'title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1'},
  {'title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1'},
  {'title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1'}
]
songs.group_by{|h| h[:album]}.inject({}){|h,(album,s)| h[album] = {:title => album, :songs => s }; h }
#=> {"Album 1"=>{:songs=>[{:artist=>"Artist 1", :album=>"Album 1", :title=>"Title 1"}, {:artist=>"Artist 1", :album=>"Album 1", :title=>"Title 3"}, {:artist=>"Artist 2", :album=>"Album 1", :title=>"Title 4"}, {:artist=>"Artist 3", :album=>"Album 1", :title=>"Title 5"}], :title=>"Album 1"}, "Album 2"=>{:songs=>[{:artist=>"Artist 2", :album=>"Album 2", :title=>"Title 2"}], :title=>"Album 2"}}

只是我不明白为什么你使用专辑标题两次:作为哈希键和作为标题参数?我认为你应该只使用一次:)

You can do it in one line

songs = [
  {'title' => 'Title 1', 'artist' => 'Artist 1', 'album' => 'Album 1'},
  {'title' => 'Title 2', 'artist' => 'Artist 2', 'album' => 'Album 2'},
  {'title' => 'Title 3', 'artist' => 'Artist 1', 'album' => 'Album 1'},
  {'title' => 'Title 4', 'artist' => 'Artist 1', 'album' => 'Album 1'},
  {'title' => 'Title 5', 'artist' => 'Artist 3', 'album' => 'Album 1'}
]
songs.group_by{|h| h[:album]}.inject({}){|h,(album,s)| h[album] = {:title => album, :songs => s }; h }
#=> {"Album 1"=>{:songs=>[{:artist=>"Artist 1", :album=>"Album 1", :title=>"Title 1"}, {:artist=>"Artist 1", :album=>"Album 1", :title=>"Title 3"}, {:artist=>"Artist 2", :album=>"Album 1", :title=>"Title 4"}, {:artist=>"Artist 3", :album=>"Album 1", :title=>"Title 5"}], :title=>"Album 1"}, "Album 2"=>{:songs=>[{:artist=>"Artist 2", :album=>"Album 2", :title=>"Title 2"}], :title=>"Album 2"}}

Only I don't understand why do you use Album title twice: as a hash key and as a title param? You should use only once, I think :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文