Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Do you know where your variables are?
 
PerlMonks  

Re: can't use certain hashnames when traversing a slightly complex hash

by Athanasius (Prior)
on Jan 06, 2013 at 11:33 UTC ( #1011878=note: print w/ replies, xml ) Need Help??


in reply to can't use certain hashnames when traversing a slightly complex hash

Works OK for me:

#! perl use strict; use warnings; my @gists = ( { files => { 'main.html' => { language => 'HTML', filename => 'main.html', type => 'text/html', size => 713, raw_url => 'THIS IS WHAT I WANT' } } } ); foreach my $g (@gists) { my @files = keys(%{$g->{files}}); my $file = $files[0]; print $g->{files}->{$file}->{raw_url}; }

Output:

21:28 >perl 472_SoPW.pl THIS IS WHAT I WANT 21:28 >

The only difference a dot in the hash key makes is that the key has to be quoted under use strict.

Can you provide a minimal example that demonstrates the problem you are seeing?

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


Comment on Re: can't use certain hashnames when traversing a slightly complex hash
Select or Download Code
Re^2: can't use certain hashnames when traversing a slightly complex hash
by gideondsouza (Pilgrim) on Jan 06, 2013 at 15:09 UTC

    Bah! This is so ridiculous, I retyped the code when I was posting the question and ended up typing the code correctly!!

    The code was originally like this:

    print ->$g->{files}->{$file}->{raw_url};

    How do you usually figure the code for a hash traversal? I kept having to restart my webapp, Do you usually use the debugger and inspect the hash? I guess I really have to get used to the command line debugger

    Thanks so much for your replies dear monks! Sorry about the trouble! This is another pretty silly issue.

      How do you usually figure the code for a hash traversal?

      Not sure what you’re asking here, but for the record, the line:

      print $g->{files}->{$file}->{raw_url};

      can be simplified to:

      print $g->{files}{$file}{raw_url};

      because “The arrow is optional between brackets subscripts” (perlref#Using-References, point 3).

      Do you usually use the debugger and inspect the hash?

      No, I’d likely use Data::Dump or Data::Dumper, as in the OP.

      Anyway, glad the problem has been sorted. :-)

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

        I ended up with that syntax issue just because I kept switching between the code and the browser and the killing/starting the server. Wondered if there was a faster way

        Anyway, it's just been less than two months since I met perl, I'll get used to Data::Dumper :)

Re^2: can't use certain hashnames when traversing a slightly complex hash
by NetWallah (Monsignor) on Jan 06, 2013 at 18:54 UTC
    Inside the loop, try this
    foreach my $g (@gists) { for my $filename( keys(%{$g->{files}}) ) { my $fref = $g->{files}{$filename}; # Simplify access to conten +ts ..( -> is optional betweeen "}{" ) print $fref->{raw_url}; last; # If you only want info for ONE file } }

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-04-21 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (491 votes), past polls