metaperl has asked for the wisdom of the Perl Monks concerning the following question:

How's that for a title? Anyway, for some reason, a function which I believe should not alter my LoH at all is adding entries to the individual hash refs. After the first use of the function, it adds:
1 => {}
After the second use of the function it has
1 => {}, 2 => {}

Code

use Data::Dumper; use EDI::OrderStatus; use strict; sub collander_LoH { my $LoH = shift; my @ret; for (@$LoH) { my $key = keys %{$_}; my $val = $_->{$key}{'length'}; push @ret, { $key => $val }; } \@ret; } our $R = [ { client_number => { element => "Collectors Client Number (Co#) ", length => 2 , decimal => "n/a ", format => 3 , justify => "L ", fill => "Blanks " } } , { delete_code => { element => "Delete Code ", length => 1 , decimal => "n/a ", format => 4 , justify => "L ", fill => "Blanks " } } ]; for (1..3) { warn Data::Dumper->Dump([$R],['R']); collander_LoH $R; }

Output (Note the added hash entries of the number N => {}, where N is an integer)

cctbrann@mcynote ~/SRC/modules/EDI/OrderStatus : perl coll.pl $R = [ { 'client_number' => { 'format' => 3, 'decimal' => 'n/a ', 'length' => 2, 'justify' => 'L ', 'fill' => 'Blanks ', 'element' => 'Collectors Client Number ( +Co#) ' } }, { 'delete_code' => { 'format' => 4, 'decimal' => 'n/a ', 'length' => 1, 'justify' => 'L ', 'fill' => 'Blanks ', 'element' => 'Delete Code + ' } } ]; $R = [ { 'client_number' => { 'format' => 3, 'decimal' => 'n/a ', 'length' => 2, 'justify' => 'L ', 'fill' => 'Blanks ', 'element' => 'Collectors Client Number ( +Co#) ' }, '1' => {} }, { 'delete_code' => { 'format' => 4, 'decimal' => 'n/a ', 'length' => 1, 'justify' => 'L ', 'fill' => 'Blanks ', 'element' => 'Delete Code + ' }, '1' => {} } ]; $R = [ { 'client_number' => { 'format' => 3, 'decimal' => 'n/a ', 'length' => 2, 'justify' => 'L ', 'fill' => 'Blanks ', 'element' => 'Collectors Client Number ( +Co#) ' }, '1' => {}, '2' => {} }, { 'delete_code' => { 'format' => 4, 'decimal' => 'n/a ', 'length' => 1, 'justify' => 'L ', 'fill' => 'Blanks ', 'element' => 'Delete Code + ' }, '1' => {}, '2' => {} } ]; cctbrann@mcynote ~/SRC/modules/EDI/OrderStatus :

Replies are listed 'Best First'.
Re: LoH Incrementing Autovivification
by merlyn (Sage) on Nov 17, 2000 at 20:21 UTC
Re: LoH Incrementing Autovivification
by metaperl (Curate) on Nov 17, 2000 at 20:24 UTC
    Ah, thank you merlyn. Of course, I don't have a good logical reason why I did it. Chalk it up to some subconscious oversight in combination with knowing that I was only going to get back one key.

    Thank you very much

      For those that don't quite get what happened:
      my $key = keys %{$_}; my $val = $_->{$key}{'length'};
      In a scalar context, keys is returning the number of keys, which is 1 when it's first called. In the second line, we're going under the assumption that $_->{1} is a valid hashref, and checking for the 'length' key of that hashref. Perl has to oblige ("autovivification"), so it creates the necessary hashref to make $_->{$key}{'length'} a legal expression, thus giving us a new key (1) in the hashref referred to in $_.