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.
  • I had to do someting similar to create a multilevel hash table that returns a list of "cached" things given a list of parameters that represent the hashing value.

    The main problem was not creating the various anonymous hashes, but that the last item is not a {} but a []. The trick was to keep a reference to the previous step, to be able to go back and turn a newly created hash reference into an anonymous array.

    # $thing is a hash reference
    # @keys is the list of $thing attributes I'm using for the hashing algorithm
    # $hashtable is a reference to the hash table itself
    my $list = $hashtable;
    my $last;
    ($last, $list) = ( $list, $list->{$thing->{$_}} ||= {} ) for @keys;
    $list = $last->{$thing->{$keys[-1]}} = [] if ref $list eq 'HASH';
    # and now $list points to the list of interesting items
    # which was created (empty) if needed

    Using $list, $last adds extra obfuscation points, but that wasn't intended, since those are fitting names.