Skip to content

Instantly share code, notes, and snippets.

@Code-Hex
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Code-Hex/133361678a6a48cd0399 to your computer and use it in GitHub Desktop.
Save Code-Hex/133361678a6a48cd0399 to your computer and use it in GitHub Desktop.
ハッシュと向き合った
#! /usr/bin/perl
use strict;
use warnings;
use 5.010;
my $n = "\n";
my %hash = ("foo", 35, "bar", 12.4, 2.5, "hello");
say $_ foreach (%hash);
print $n;
my %hash2 = ("foo" => "test", "hello" => "banana", "Night" => "Sleep", "Morning" => "Yahoo!");
say $_ foreach (%hash2); # 要素順に行う訳ではない
my @keys = keys %hash2; # 要素順に格納される訳ではない
my @values = values %hash2; # 要素順に格納される訳ではない
print $n;
say $_ foreach (@keys);
print $n;
say $_ foreach (@values);
print $n;
# each という関数がある。
# これは主にこのような形で考える
# もちろん要素順に行う訳ではない
while (my ($key, $value) = each %hash2){
say "$key => $value";
}
print $n;
print "PATH is $ENV{PATH}\n"; # %ENV ハッシュ
=
result
bar
12.4
foo
35
2.5
hello
hello
banana
Morning
Yahoo!
foo
test
Night
Sleep
hello
Morning
foo
Night
banana
Yahoo!
test
Sleep
hello => banana
Morning => Yahoo!
foo => test
Night => Sleep
PATH is /usr/local/share/python:/Users/codehex/.rbenv/shims:/Users/codehex/.plenv/shims:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment