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

I am trying to simulate my real case scenario here.
#!/usr/bin/perl use strict; use warnings; my %Names = { 'Washington' => { 'name' => ['Eric','Smith'] } 'California' => { 'name' => ['John','Paddy','Josh'] } }; my %Cars = { 'Washington' => 'Honda', 'California' => 'BMW' };

I want the following output

Washington: Eric drives Honda Smith drives Honda California: John drives BMW Paddy drives BMW Josh drives BMW

Please note that each names should be newline. I know that this can be written as "Honda is driven by Eric and Smith" , but my scenario is different. I want the lines to print exactly as stated above. Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: looping multidimensional arrays
by BrowserUk (Patriarch) on Dec 05, 2013 at 08:54 UTC

    for my $key ( keys %Cars ) { printf "%10.10s: %s\n\n", $key, join "\n ", map{ "$_ drives $Cars{ $key }" } @{ $Names{ $key }{name} }; };; Washington: Eric drives Honda Smith drives Honda California: John drives BMW Paddy drives BMW Josh drives BMW

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks guys, I never left empty handed, since I met you guys. You rock !!

Re: looping multidimensional arrays
by Random_Walk (Prior) on Dec 05, 2013 at 14:15 UTC

    I would be inclined to alter the data structure and end up with something like this:

    #!/usr/bin/perl use strict; use warnings; my %data = ( 'Washington' => { 'car' => 'Honda', 'names' => ['Eric','Smith'] }, 'California' => { 'car' => 'BMW', 'names' => ['John','Paddy','Josh'] } ); for my $state (sort keys %data) { my $car = $data{$state}{car}; for my $name ( @{ $data{$state}{names} } ) { $state .= ":" if $state ne ''; printf "%12s %s drives %s\n", $state, $name, $car; $state = ''; } }

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: looping multidimensional arrays (the same as not looping them)
by Anonymous Monk on Dec 05, 2013 at 08:48 UTC

    Access is the same whether you're looping or not

    my %State; ## in the Same-of-Washington there is an eric $State{Washington}{name}[0] = "Eric"; #d2 $State{Washington}{name}[1] = "Smith"; #d2 $State{California}{name}[0] = "John"; #d2 $State{California}{name}[1] = "Paddy"; #d2 $State{California}{name}[2] = "Josh"; #d2 my %Car; ## Car-of-Washington $Car{Washington} = "Honda"; #d0 $Car{California} = "BMW"; #d0

    See also references quick reference

    see also

    chromatics free book Modern Perl a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.
    Learn Perl in about 2 hours 30 minutes