Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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!

Replies are listed 'Best First'.
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'
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};

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://987906]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2017-08-21 18:08 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (324 votes). Check out past polls.