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

Hi there Monks!
I am trying to add data to my an existing file and I can't figure this one out. It is adding the data I want, but with problems, one is that it is adding at he end of the file this "undef" stuff, I dont know why its adding it there. The second is, the value I need its been added, but I am getting this "Odd number of elements in anonymous hash at add_to_array.pl line 28." Here is the code with my issues.
#!/usr/bin/perl -w use strict; use CGI qw(-oldstyle_urls :standard); use Data::Dumper; use Date::Format; use Date::Parse; my $q = new CGI; $| = 1; my @data; my $today = get_timezone(); #set $Data::Dumper::Purity to 1 if you have nested references #$Data::Dumper::Purity = 1; open (FILE, ">data.txt") or die "can't open file: $!"; my $weather_data = do "data.txt"; for my $w_data (@$weather_data) { push @data, \$w_data; } push @data, {$data[0]{'xml_api_reply'}{'weather'}{'report_date'}->{'-d +ata'} = $today,}; print FILE Dumper(\@data); close FILE or die "can't close file: $!"; sub get_timezone { ### Get our local time. $ENV{'TZ'} = 'America/New_York'; $today = time2str( "%m-%d-%Y \@ %H:%M:%S", time); }

The data:
$VAR1 = [ { 'xml_api_reply' => { '-version' => '1', 'weather' => { '-row' => '0', 'current_conditions' = +> { + 'icon' => { + '-data' => '/ig/images/weather/mostly_cloudy.gif' + }, + 'temp_f' => { + '-data' => '70' + }, + 'temp_c' => { + '-data' => '21' + }, + 'wind_condition' => { + '-data' => 'Wind: E at 7 mph' + }, + 'humidity' => { + '-data' => 'Humidity: 65%' + }, + 'condition' => { + '-data' => 'Mostly Cloudy' + } + }, } } }, ];

Thanks for the help!

Replies are listed 'Best First'.
Re: Odd number of elements in anonymous hash!
by NetWallah (Canon) on Aug 19, 2012 at 17:37 UTC
    You probably intended the hash ref constructor to read:
    {$data[0]{'xml_api_reply'}{'weather'}{'report_date'}->{'-data'} => $to +day,}
    (Changed the = to =>)

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      If I do what that thats what I am getting:
      push @data, {$data[0]{'xml_api_reply'}{'weather'}{'report_date'}->{'-d +ata'} => $today,};

      Use of uninitialized value in anonymous hash ({}) at add_to_array.pl line 28.
        It is not clear if you are trying to push a SCALAR or HASHREF into @data.

        If you want a SCALAR, take out the outermost {}, and use an assignment instead of my recommended =>.

        You may want to remove the "push" statement from the code entirely, because with it, you are entering the same information twice, into @data.

                     I hope life isn't a big joke, because I don't get it.
                           -SNL