Dynamically/recursively building hashes in Perl?

前端 未结 5 1129
北恋
北恋 2020-12-19 04:45

I\'m quite new to Perl and I\'m trying to build a hash recursively and getting nowhere. I tried searching for tutorials to dynamically build hashes, but all I could find wer

5条回答
  •  自闭症患者
    2020-12-19 05:02

    Data::Diver covers this niche so well that people shouldn't reinvent the wheel.

    use strict;
    use warnings;
    use Data::Diver 'DiveVal';
    use Data::Dumper;
    
    my $root = {};
    while ( my $line =  ) {
        chomp($line);
        DiveVal( $root, split m!/!, $line ) = '';
    }
    print Dumper $root;
    __DATA__
    one/two/three
    four
    five/six/seven/eight
    

提交回复
热议问题