Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Not a HASH reference using YAML

by Anonymous Monk
on Aug 17, 2012 at 03:01 UTC ( #987906=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!
I am trying to open a file(YAML) and it is giving an Software Error "Not a HASH reference at", what am I doing wrong here?
... open my $fh, '<', 'data.txt' or die "can't open config file: $!"; my @config = YAML::LoadFile($fh); #print Dumper(@config); for my $record (@config) { my $cities = "$record->{'xml_api_reply'}{'weather'}{'forecast_inf +ormation'}{'city'}->{'-data'}"; my $currwind ="$record->{xml_api_reply}->{weather}->{current_cond +itions}->{wind_condition}->{'-data'}" || ''; #for my $subrecord (@{$record->{Field2}}) { #print "\t$subrecord->{Key} = $subrecord->{Val}\n"; #} print "**$cities*$currwind*\n"; }
Thanks for looking!

Comment on Not a HASH reference using YAML
Download Code
Re: Not a HASH reference using YAML
by rpnoble419 (Pilgrim) on Aug 17, 2012 at 05:53 UTC
    The Loadfile function will open and read the file for you. See the code below....
    #!/usr/bin/perl use YAML qw(LoadFile); my %Config; %Config=LoadFile("YAMLFile.txt"); print $Config{Item1}{SubItem1};
Re: Not a HASH reference using YAML
by tobyink (Abbot) on Aug 17, 2012 at 06:12 UTC

    You are trying to treat something as a hash reference (i.e. doing this: ->{foobar}) but it's not a hash reference; it's some other kind of reference. (Given that the source of the data is YAML, I'd put my money on it being an array reference.)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      How would you do it if it is an array reference? Thanks!

        Array refs use square brackets rather than curly braces; and numeric indices rather than strings. So something like ->[0].

        Of course, I haven't seen your data, so it's impossible for me to say at what point you need to do this.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2015-07-07 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (91 votes), past polls