LoginSignup
15
14

More than 5 years have passed since last update.

ハッシュのスライス

Posted at

ハッシュに対し、指定した複数のキーに対応する値たちを取得する方法。

Perlではこんな書き方。

#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);

my %hash = (
    hoge => 'foo',
    fuga => 'bar',
    piyo => 'baz',
);

for my $val (@hash{qw(piyo fuga)}) {
    say $val;
}

RubyではHash#values_atが使えるらしい。

#!/usr/bin/env ruby

hash = {
  :hoge => 'foo',
  :fuga => 'bar',
  :piyo => 'baz',
}

hash.values_at(:piyo, :fuga).each do |val|
  puts val
end
15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14