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

Re^2: Adding row in a hash Help!!

by Anonymous Monk
on Aug 20, 2012 at 02:27 UTC ( #988376=note: print w/replies, xml ) Need Help??


in reply to Re: Adding row in a hash Help!!
in thread Adding row in a hash Help!!

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!

Replies are listed 'Best First'.
Re^3: Adding row in a hash Help!!
by davido (Archbishop) on Aug 20, 2012 at 03:02 UTC

    ...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

Re^3: Adding row in a hash Help!!
by hbm (Hermit) on Aug 20, 2012 at 02:46 UTC

    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.

      It runs, but look at the extra line added to end end of the file:
      ... $VAR1->[1] = $VAR1->[0]{'xml_api_reply'}{'weather'}{'report_date'};

        Ah, ok. Try these changes:

        # no need for this loop: # for my $w_data (@$weather_data) { # push @data, $w_data; # } # Now add 'report_date' key and its value at the appropriate level. #push @data, $weather_data->[0]{'xml_api_reply'}{'weather'}{'report_da +te'} = { '-data' => $today #}; push @{$weather_data->[0]{'xml_api_reply'}{'weather'}{'report_date'}}, + { '-data' => $today }; # print Dumper(\@data); print Dumper(\$weather_data);
      Here is the answer:
      $data[0]{'xml_api_reply'}{'weather'}{'report_date'} = {'-data' => $tod +ay };

      No need to use push!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://988376]
help
Chatterbox?
[Eily]: It can also be used to force a copy, and remove magic from a variable
[Eily]: there was a post some time ago about variable coming from regex taking more space than the same string defined directly IIRC
[Eily]: and if there's XS, make sure the scalar holds a string representation, that sounds like bad practice though (forcing that on the call side)
[choroba]: You should never pass $1 without double quotes to a sub
[choroba]: or "I should never", at least
[choroba]: that's not the sub's business

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2018-02-22 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (296 votes). Check out past polls.

    Notices?