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

The following code works. However, I am thinking there is a more idiomatic way of accomplishing the end result:
my @cmd_choices; my %speedlist = %{$heap->{config}{freq_gui}{speedlist}}; foreach my $key (keys %speedlist) { push @cmd_choices, { -name => $key, -value => $speedlist{$key}}; }

Replies are listed 'Best First'.
Re: Idiomize This - Hash to Array of Hashes
by jettero (Monsignor) on Apr 20, 2009 at 01:03 UTC
    my $speedlist = $heap->{config}{freq_gui}{speedlist}; # (updated, see +AnomalousMonk below) push @cmd_choices, map { {name=>$_, value=>$speedlist->{$_}} } keys %$ +speedlist;

    my ($k,$v); push @c, {n=>$k, v=>$v} while ($k,$v) = each %$speedlist;

    -Paul

      The push isn't necessary here no?
      my $speedlist = ....; my @cmd_choices = map {{-name=>$_, -value=>$speedlist->{$_}}} keys %$s +peedlist;
        Thanks to both of you:
        my $speedlist = $heap->{config}{freq_gui}{speedlist}; my @cmd_choices = map {{-name=>$_, -value=>$speedlist->{$_}}} keys + %$speedlist;
        It wasn't clear to me if the @cmd_choices already had data in it. If it did, the push was necessary, otherwise, no.

        -Paul

      my $speedlist = ....;
      The OPer may not be aware that you are now creating and dealing directly with a hash reference, and that the full statement would be
      my $speedlist = $heap->{config}{freq_gui}{speedlist};
      Update: The OPer may not be aware ...     I take that back. Evidently, he is.
        I wasn't aware of it until it wouldn't compile!

        Thanks,

        John