Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Adding row in a hash Help!!

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

Hi Monks I am in a similar problem as I've seeing here. I am trying to insert new data to this array of hashes and I cant get it, any help please?!
Data before:
VAR1 = [ { 'xml_api_reply' => { '-version' => '1', 'weather' => { '-row' => '0', 'current_conditions' = +> { ...

Data after adding the new element:
VAR1 = [ { 'xml_api_reply' => { '-version' => '1', 'weather' => {'report_date' => { '-d +ata' => '08-19-2012 @ 15:40:26' }, '-row' => '0', 'current_conditions' = +> { ...

This is what I am trying to insert:
'report_date' => { '-d +ata' => '08-19-2012 @ 15:40:26' },

Thanks a lot for looking!!

Comment on Adding row in a hash Help!!
Select or Download Code
Re: Adding row in a hash Help!!
by davido (Archbishop) on Aug 20, 2012 at 01:58 UTC

    This ought to do it. perldsc.

    use Data::Dumper; # So that we can inspect our result. # Initial state. my $aref = [ { 'xml_api_reply' => { '-version' => '1', 'weather' => { '-row' => '0', 'current_conditions' => { } }, }, }, ]; # Now add 'report_date' key and its value at the appropriate level. $aref->[0]{'xml_api_reply'}{'weather'}{'report_date'} = { '-data' => ' +08-19-2012 @ 15:40:26' }; # Inspect the result. print Dumper $aref;

    Dave

      Hi Dave, any ideas why it does not work with more data like in this code?
      #!/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; use vars qw($vars); $| = 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; } # Now add 'report_date' key and its value at the appropriate level. push @data, $data->[0]{'xml_api_reply'}{'weather'}{'report_date'} = { +'-data' => $today }; print Dumper(\@data); #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); }

      data.txt
      $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 trying to help!

        And what does, "does not work" mean this time?

        I got a pretty clear error message when running your script:

        Global symbol "$data" requires explicit package name ...

        Which led me to this line:

        push @data, $data->[0]{'xml_api_reply'}{'weather'}{'report_date'} = {' +-data' => $today };

        Which I changed to this:

        push @data, $weather_data->[0]{'xml_api_reply'}{'weather'}{'report_dat +e'} = {'-data' => $today };

        Now it runs without errors.

        ...any ideas why it does not work with more data like in this code?

        Because you pasted code without taking the time to understand what it's doing.

        There are resources available: perlintro, perlref, perlreftut, and perldsc.


        Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-12-25 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (162 votes), past polls