Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

how to print out side of foreach loop($k1 $a{$k1}) ?

by virudinesh (Acolyte)
on May 29, 2013 at 05:40 UTC ( #1035781=perlquestion: print w/ replies, xml ) Need Help??
virudinesh has asked for the wisdom of the Perl Monks concerning the following question:

how to print out side of foreach loop($k1 $a{$k1}) ?

my %a=(); $a{1}{"a"}{"A"}="FIRST"; $a{1}{"c"}{"B"}="THIRD"; $a{1}{"b"}{"C"}="SECOND"; foreach my $k1 ( sort keys %a ) { foreach my $k2 ( sort keys %{$a{$k1}} ) { foreach my $k3 ( sort keys %{$a{$k1}{$k2}} ) { print "$a{$k1}{$k2}{$k3}\n"; } } }

its print hash variable only inside of foreach loop

i want to print outside of loop in $a{$k1}{$k2}{$k3} this variable

Comment on how to print out side of foreach loop($k1 $a{$k1}) ?
Download Code
Re: how to print out side of foreach loop($k1 $a{$k1}) ?
by kcott (Abbot) on May 29, 2013 at 06:05 UTC

    G'day virudinesh,

    Perhaps you could clarify your intent or, at least, the rationale behind what you want. Currently, you're doing this:

    $ perl -Mstrict -Mwarnings -E ' my %a=(); $a{1}{"a"}{"A"}="FIRST"; $a{1}{"c"}{"B"}="THIRD"; $a{1}{"b"}{"C"}="SECOND"; foreach my $k1 ( sort keys %a ) { foreach my $k2 ( sort keys %{$a{$k1}} ) { foreach my $k3 ( sort keys %{$a{$k1}{$k2}} ) { print "$a{$k1}{$k2}{$k3}\n"; } } } ' FIRST SECOND THIRD

    Did you perhaps want something like this:

    $ perl -Mstrict -Mwarnings -E ' my @items_to_print_outside_loop; my %a=(); $a{1}{"a"}{"A"}="FIRST"; $a{1}{"c"}{"B"}="THIRD"; $a{1}{"b"}{"C"}="SECOND"; foreach my $k1 ( sort keys %a ) { foreach my $k2 ( sort keys %{$a{$k1}} ) { foreach my $k3 ( sort keys %{$a{$k1}{$k2}} ) { push @items_to_print_outside_loop, "$a{$k1}{$k2}{$k3}"; } } } print join("\n", @items_to_print_outside_loop), "\n"; ' FIRST SECOND THIRD

    or like this:

    $ perl -Mstrict -Mwarnings -E ' sub print_outside_loop { print "@_\n" } my %a=(); $a{1}{"a"}{"A"}="FIRST"; $a{1}{"c"}{"B"}="THIRD"; $a{1}{"b"}{"C"}="SECOND"; foreach my $k1 ( sort keys %a ) { foreach my $k2 ( sort keys %{$a{$k1}} ) { foreach my $k3 ( sort keys %{$a{$k1}{$k2}} ) { print_outside_loop($a{$k1}{$k2}{$k3}); } } } ' FIRST SECOND THIRD

    Update: removed -Mdiagnostics (3 instances). A cut-and-paste error which got accidentally propagated. It doesn't hurt but it's not needed and might be confusing with respect to the reason for its inclusion. (Oops! Update of update: s/It does hurt/It doesn't hurt/)

    -- Ken

Re: how to print out side of foreach loop($k1 $a{$k1}) ?
by vinoth.ree (Parson) on May 29, 2013 at 06:08 UTC
    Hi virudinesh

    I do not think i understand you requirement clearly.

    As you have hash of hashes you need to use loops to iterate over each of them and print the values.

    If you do not want to use loop means we have Data::Dumper module to print the data structure.


    All is well

      Yes, Data::Dumper is another potential candidate; however, the OP probably also wants to set $Data::Dumper::Sortkeys.

      -- Ken

Re: how to print out side of foreach loop($k1 $a{$k1}) ?
by davido (Archbishop) on May 29, 2013 at 06:16 UTC

    If you want $k1, $k2, and $k3 to still be in scope after the loops terminate, declare them at a broader lexical scope than the confines of the loops. The only way to print them after the loops terminate is for them to still be in scope after the loops terminate, which means they have to be declared outside of the loops.


    Dave

Re: how to print out side of foreach loop($k1 $a{$k1}) ?
by Anneq (Vicar) on May 29, 2013 at 11:03 UTC

    You shouldn't use $a as variable. $a and $b are special variables in perl, used to hold results from the sort function.

    As stated by others, use the Data::Dumper module for printing hashes.

    use Data::Dumper; my %not_a_or_b=(); $not_a_or_b{1}{"a"}{"A"}="FIRST"; $not_a_or_b{1}{"c"}{"B"}="THIRD"; $not_a_or_b{1}{"b"}{"C"}="SECOND"; foreach my $k1 ( sort keys %not_a_or_b ) { foreach my $k2 ( sort keys %{$not_a_or_b{$k1}} ) { foreach my $k3 ( sort keys %{$not_a_or_b{$k1}{$k2}} ) { print "$not_a_or_b{$k1}{$k2}{$k3}\n"; } } } print Dumper(\%not_aor_b);

    Anne

      Nope:

      Global symbol "%not_aor_b" requires explicit package name at derp.pl l +ine 20. Execution of derp.pl aborted due to compilation errors.

      Consider running the code, or copying and pasting your actual code, to catch such errors.

        Thanks again, marto,

        I just wanted to HELP him, by indicating what was going wrong for him.

        Vowing to never try to help out perl monks while trying to get kids on the bus in the morning, before my first cup of coffee. Will test my code next time.

        Anne

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1035781]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2014-08-29 17:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (283 votes), past polls