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

How To Print Single Element From Nested Hashes?

by jdlev (Scribe)
on Sep 10, 2013 at 21:23 UTC ( #1053366=perlquestion: print w/ replies, xml ) Need Help??
jdlev has asked for the wisdom of the Perl Monks concerning the following question:

Hi again guys. I really am sorry about asking so many questions, but really appreciate the help...

So right now, in my program I have basically have a ton of data that repeats itself, and I'm trying to condense it to speed it up. It get's the same stats on every player. PlayerID, Position, Salary, PointsPerGame...I'm trying to figure out how to explain this, so bear with me because I'm utterly confused...

I have a foreach loop that iterates through a DBI produced Hash. It does it based on the players position. So basically, it looks kind of like this:

foreach $QB1(sort keys $Hash) { $QB1 = $points; foreach $QB2 (sort keys $Hash) { $QB2 = $points; foreach $RB1 (sort keys $Hash) { $RB1 = $points; foreach $RB2 (sort keys $Hash) { $RB2 = $points; }#end $RB2 foreach loop }#end $RB1 foreach loop }#end $QB2 foreach loop }#end $QB1 foreach loop

Now I know I should be able to condenses 10 foreach loops into a single one, but how? Should I use some type of subroutine? Dynamic Variables fed by arrays?

How would I make dynamic variables where on the iterations through the formula, the variable name is updated. So for instance, the first time through, the variable would be set to $QB1, the second time through, it would be set to $QB2...and so on?

Thanks for any help once again :)

Update

Let's say a hash has the following structure:

'113' => { 'Name'=>'John', 'Age'=>'33', 'Sex'=>'Yes Please' }, '114' => { 'Name'=>'Melissa', 'Age'=>'25', 'Sex'=>'No Way' }
What syntax would I use to pull out the 'Name' for '114'? to have the program return 'Melissa'?

I love it when a program comes together - jdhannibal

Comment on How To Print Single Element From Nested Hashes?
Select or Download Code
Re: Dynamic Variables?
by Anonymous Monk on Sep 10, 2013 at 22:19 UTC
    Put the data in appropriate tables in an SQL database or SQLite and you won't have to write a program at all.
Re: Dynamic Variables?
by tobyink (Abbot) on Sep 10, 2013 at 23:14 UTC

    List::MapMulti?

    use List::MapMulti qw(mapm); my @keys = sort keys %hash; mapm { my ($QB1, $QB2, $RB1, $RB2) = @_; ...; } \@keys, \@keys, \@keys, \@keys;

    ... or even...

    use List::MapMulti qw(mapm); my @keys = sort keys %hash; mapm { my ($QB1, $QB2, $RB1, $RB2) = @_; ...; } (\@keys) x 4;
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: How To Print Single Element From Nested Hashes? (simple answer)
by tye (Cardinal) on Sep 11, 2013 at 00:41 UTC
    my $melissa = $hash{114}{Name}; # or $melissa = $hash_ref->{114}{Name};

    - tye        

Re: How To Print Single Element From Nested Hashes?
by AnomalousMonk (Abbot) on Sep 11, 2013 at 01:08 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-12-25 14:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls