Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • The only real innovation is using only one hash, and using the tier indices to avoid the backing up.

    use strict;
    use warnings;

    use Data::Dumper;

    my $hash = {};

    set_multi_level_hash($hash, [qw/ a b c d /], 'potato');

    print Dumper $hash;

    sub set_multi_level_hash
          {
          my ( $hash, $tiers, $value ) = @_;

          for (0 .. $#$tiers)
                {
                $hash->{$tiers->[$_]} ||= ( $_ == $#$t
    • Yeah, I thought about something like that, but then I should go through 0..$#$tiers-1, and do the special case for the last value.

      Oh well, too late now. It is out of my hands.

      Thanks for the code :)
      • I think one needs to specify what happens in this case:

        set_multi_level_hash($h, [qw(a b c)], 42);
        set_multi_level_hash($h, [qw(a b)], 66);

        Oops, we just nuked the first value and the whole "subhash".

        In other words, is the level of multiness going to stay the same?

        Personally, I would either create a class that guards against the multiness changing, or would make the data structure to be an array reference of hash references, keyed off in the array reference on the depth of $tiers (the latter can of course
        • What you are "supposed" to get depends on what your requirements are. In this case, you assign a different value to a hash key, you replace the value, just like you do if you had written it out as "$h->{a}{b} = 42".

          I suppose you could create a class to limit operations, but then, you probably would have need ed a lot more time to deliver the code. One of the requirements was "right now". I didn't build in a lot of extra stuff to make variables not be variable.

          There is no get_multi_level_hash function, so it shouldn't return anything. If you write one, you can decide what it should do. :)