LoginSignup
11
11

More than 5 years have passed since last update.

文字列からハッシュに変換

Posted at

ググると二回ループを回す例が多いですが、一回で出来ます。

string_to_hash.pl
use Data::Dumper;
# この例では「,」がレコードの区切り、「:」がキーと値の区切り
my $string = "1:abc,2:def,3:ghi,4:jkl";

my %hash = split(/[:,]/, $string)

print Dumper \%hash;

実行結果

$VAR1 = {
          '4' => 'jkl',
          '1' => 'abc',
          '3' => 'ghi',
          '2' => 'def'
        };

ハッシュなので順番は保障されません。

11
11
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
11
11