Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

A "simple" question regarding accessing a value in a complex data structure.

by Anonymous Monk
on Oct 09, 2013 at 06:59 UTC ( [id://1057491]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks. In the following code:
use strict; use warnings; my %tgs = ( 'articles' => { 'Java' => 'How to Program in Java', 'Perl' => 'Perl Programming for Biologists', 'Python' => { 'Programming' => { q/Expert's Guide to Scala +/ }, }, }, 'eBooks' => { 'Linux' => q/Linux: A Beginner's Guide/, }, ); print $tgs{articles}{Python}{Programming};
I am attempting to display "Expert's Guide to Scala" (no quotes). The preceding print statement was one of several unsuccessful attempts. The odd number of hash elements warnings arises from the fact that there is only one key? Thanks.

Replies are listed 'Best First'.
Re: A "simple" question regarding accessing a value in a complex data structure.
by choroba (Cardinal) on Oct 09, 2013 at 07:46 UTC
    The problem is here:
    'Programming' => { q/Expert's Guide to Scala/ },
    Curly brackets introduce an anonymous hash, but there is only one element inside: q/Expert's Guide to Scala/. Remove the curlies.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: A "simple" question regarding accessing a value in a complex data structure.
by wjw (Priest) on Oct 09, 2013 at 07:08 UTC
    Try this,
    use strict; use warnings; my %tgs = ( 'articles' => { 'Java' => 'How to Program in Java', 'Perl' => 'Perl Programming for Biologists', 'Python' => { 'Programming' => q/Expert's Guide to Scala/ +, }, }, 'eBooks' => { 'Linux' => q/Linux: A Beginner's Guide/, }, ); print $tgs{articles}{Python}{Programming};
    Hope that helps,
    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results.
Re: A "simple" question regarding accessing a value in a complex data structure.
by Anonymous Monk on Oct 09, 2013 at 07:45 UTC

    The odd number of hash elements warnings arises from the fact that there is only one key? Thanks

    Yes, a key without a value, specifically  { q/Expert's Guide to Scala/ },

    my $hashref = { 'hasherefs', 'are', 'made', 'with', 'curly', 'braces', 'they', 'like', 'to', 'come', 'in', 'pairs' };

    Here is another way to initialize , the rehohy way

    my %tgs; $tgs{articles}{Python}{Programming}{"Expert's Guide to Scala"} = undef; #d3 $tgs{articles}{Perl} = "Perl Programming for Biologists"; #d1 $tgs{articles}{Java} = "How to Program in Java"; #d1 $tgs{eBooks}{Linux} = "Linux: A Beginner's Guide"; #d1

    And wouldn't you know it, its also a way to access certain elements :)

    See also references quick reference

Re: A "simple" question regarding accessing a value in a complex data structure.
by AlexTape (Monk) on Oct 09, 2013 at 08:36 UTC
    after fixing the brackets like choroba introduced,
    i would access and travel the hash like this..
    #!"C:\perl\bin\perl.exe" -w # pragma use strict; use warnings; use diagnostics; # includes use Data::Dumper; use Data::Diver qw( Dive ); # data my ( $tgs, %tgs ); %tgs = ( 'articles' => { 'Java' => 'How to Program in Java', 'Perl' => 'Perl Programming for Biologists', 'Python' => { 'Programming' => q/Expert's Guide to +Scala/, }, }, 'eBooks' => { 'Linux' => q/Linux: A Beginner's Guide/, }, ); $tgs = \%tgs; # dive hash print Dumper $tgs->{articles}->{Python}->{Programming}; # returns: # # $VAR1 = 'Expert\'s Guide to Scala'; # # or # print Dumper Dive( $tgs, qw ( articles Python Programming ) ); # returns: # # $VAR1 = 'Expert\'s Guide to Scala'; 1;
    kindly perlig

    $perlig =~ s/pec/cep/g if 'errors expected';
Re: A "simple" question regarding accessing a value in a complex data structure.
by Anonymous Monk on Oct 09, 2013 at 19:25 UTC
    If the data structure is complex and especially if it is somewhat arbitrary, such that you do not wish to mirror the complexity of the data-structure in the complexity of your code, it's a good idea to look at http://search.cpan.org for the keys diver and walker.
Re: A "simple" question regarding accessing a value in a complex data structure.
by gurung (Sexton) on Oct 09, 2013 at 12:48 UTC
    print keys $tgs{articles}{Python}{Programming};

      Re #1057536:

      print keys $tgs{articles}{Python}{Programming}; # Type of argument to keys on reference must be unblessed hashref or a +rrayref ....
        As choroba commented, i changed anonymous hash to anonymous array, so no key pair value. 'Programming' => q/Expert's Guide to Scala/ , and then, print @{$tgs{articles}{Python}{Programming}};

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found