Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

hash of hashes

by Anonymous Monk
on Oct 05, 2013 at 03:55 UTC ( [id://1056998]=perlquestion: print w/replies, xml ) Need Help??

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

i have a list like X A B,want to print like A X,B X i tried many thing..can any one help me.. plz

Replies are listed 'Best First'.
Re: hash of hashes
by Athanasius (Archbishop) on Oct 05, 2013 at 04:31 UTC

    I’m guessing that — contrary to the title — “a list like X A B” is actually a hash in which the value corresponding to the key “X” is an anonymous array containing the elements “A” and “B”. If this guess is correct, the following should give the output required:

    #! perl use strict; use warnings; my %hash = ( X => [ qw( A B ) ], Y => [ qw( C D E ) ], Z => [ qw( F G H ) ], ); for my $key (sort keys %hash) { my $array_ref = $hash{$key}; print "$_ $key, " for @$array_ref; }

    Output:

    14:29 >perl 737_SoPW.pl A X, B X, C Y, D Y, E Y, F Z, G Z, H Z, 14:29 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: hash of hashes
by kcott (Archbishop) on Oct 05, 2013 at 04:51 UTC

    As others have stated, it is unclear what you want here. Your title and text seem to have no relationship to each other. Please read the guidelines in "How do I post a question effectively?" and follow them before posting again.

    My best guess at what you're after:

    #!/usr/bin/env perl -l use strict; use warnings; my @x = qw{X A B}; my $x = shift @x; my @y = map { [ $_ ] } @x; push @$_, $x for @y; print join ',' => map { "@$_" } @y;

    Output:

    A X,B X

    -- Ken

      Or, perhaps a simpler alternative demonstrated under the Perl debugger:

      DB<1> @x = qw{X A B}; DB<2> $x = shift @x; DB<3> print map {" $_ $x,"} @x; A X, B X,
Re: hash of hashes
by boftx (Deacon) on Oct 05, 2013 at 04:06 UTC

    Well, other than going into a discussion of the Associative Property, I (and I'm sure many others) have no clue what you are asking.

    On time, cheap, compliant with final specs. Pick two.
Re: hash of hashes
by Anonymous Monk on Oct 05, 2013 at 04:05 UTC

    i tried many thing..

    Can you show what you tried?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found