http://qs1969.pair.com?node_id=11151324

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

Please, I must get the $HoH values, could you help me?

for my $dia (sort keys %HoH){ print "<br>$dia: <br>"; for my $horarios (sort keys %{$HoH {$dia}}){ print "$horarios=>" . $HoH{$dia}->{$horarios} . "<br>" +; } } print "VALUE:<br>"; print ${"2023-01-01"}{"00:00"}; #I couldn't see the value
Please, will you help me to find the error? Thanks a lot. Regards.

Replies are listed 'Best First'.
Re: Print %HoH values
by Corion (Patriarch) on Mar 29, 2023 at 18:48 UTC

    Please let Perl help you and add the following two lines to the top of your script:

    use strict; use warnings;

    With that, Perl will tell you that you are trying to use a variable named 2023-01-01, which isn't really allowed in your current configuration of strict.

    Most likely, you wanted to print a value from %HoH:

    print $HoH{"2023-01-01"}{"00:00"}; #I couldn't see the value
Re: Print %HoH values
by Marshall (Canon) on Mar 30, 2023 at 17:40 UTC
    I fixed your code below. You were very close. Always use strict; and use warnings;.

    I also showed 2 popular ways of having Perl dump the structure for you. Data::Dumper is a CORE module and will always be available. Data::Dump will usually be available. Note that pp outputs to stderr and so I had to turn the buffering of stdout off in order for the printout to come out in the expected order.

    use strict; use warnings; use Data::Dump qw(pp); use Data::Dumper; $|=1; #turn off stdio buffering my %HoH = ( "2023-01-01" => { "00:00" => 1234, "01:00" => 789, "02:00" => 456, }, "2023-02-01" => { "00:00" => 4321, "01:00" => 987, "02:00" => 654, },); for my $dia (sort keys %HoH){ print "<br>$dia: \n"; for my $horarios (sort keys %{$HoH {$dia}}){ print "$horarios=>" . $HoH{$dia}{$horarios} . "\n"; } } print "VALUE:<br>"; print $HoH{"2023-01-01"}{"00:00"}; #I couldn't see the value print "\n\n"; pp \%HoH; print "\n\n"; print Dumper \%HoH; __END__ Prints: br>2023-01-01: 00:00=>1234 01:00=>789 02:00=>456 <br>2023-02-01: 00:00=>4321 01:00=>987 02:00=>654 VALUE:<br>1234 { "2023-01-01" => { "00:00" => 1234, "01:00" => 789, "02:00" => 456 }, "2023-02-01" => { "00:00" => 4321, "01:00" => 987, "02:00" => 654 }, } $VAR1 = { '2023-02-01' => { '01:00' => 987, '02:00' => 654, '00:00' => 4321 }, '2023-01-01' => { '00:00' => 1234, '02:00' => 456, '01:00' => 789 } };
Re: Print %HoH values
by Bod (Parson) on Mar 29, 2023 at 23:34 UTC