Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Assuming that the "Sample Data" in the OP was produced by a statement like
    print Dumper @$data;
then there are no arrayrefs in $data; rather, $data is a reference to an array containing elements that are hash references.

The
    foreach my $row (@$data) { ... }
loop of the OPed code iterates over the elements of the array referenced by $data; these elements are hash references. The $row scalar is aliased in turn to each hash reference and thus becomes a hash reference.

The
    if ($row->{ 'status' } eq "houses") { ... }
conditional block within the for-loop of the OPed code suggests you want to extract certain values from each referenced hash for which  $row->{ 'status' } eq "houses" is true, and your statement here suggests you want to then add the most recently extracted set of values back into all of the hash references of the array. This doesn't seem to make any sense, but it can be done:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dumper -le "my $data = [ { Ad1 => '20 SOUTH CENTRAL #B3', status => 'Property', City => 'NY +', zCode => '0002', name => 'John D' }, { Ad1 => '15 SOUTH CENTRAL #B4', status => 'Property', City => 'NY +', zCode => '0002', name => 'John V' }, { Ad1 => '100 main St.', status => 'houses', City => 'BO +', zCode => '0007', name => 'Mary' }, ]; ;; my %last_houses; ;; foreach my $hr_row (@$data) { if ($hr_row->{ 'status' } eq 'houses') { @last_houses { qw(new_name new_ad1 new_City new_z_code) } = @{ $hr_row }{ qw(name Ad1 City zCode ) }; } } ;; foreach my $hr_row (@$data) { %$hr_row = (%$hr_row, %last_houses); } ;; print Dumper $data; " $VAR1 = [ { 'new_City' => 'BO', 'status' => 'Property', 'name' => 'John D', 'City' => 'NY', 'Ad1' => '20 SOUTH CENTRAL #B3', 'new_ad1' => '100 main St.', 'new_z_code' => '0007', 'zCode' => '0002', 'new_name' => 'Mary' }, { 'new_City' => 'BO', 'status' => 'Property', 'name' => 'John V', 'City' => 'NY', 'Ad1' => '15 SOUTH CENTRAL #B4', 'new_ad1' => '100 main St.', 'new_z_code' => '0007', 'zCode' => '0002', 'new_name' => 'Mary' }, { 'new_City' => 'BO', 'status' => 'houses', 'name' => 'Mary', 'City' => 'BO', 'Ad1' => '100 main St.', 'new_ad1' => '100 main St.', 'new_z_code' => '0007', 'zCode' => '0007', 'new_name' => 'Mary' } ];
Again, please consider if this makes any sense.

Update: Also, please consider what output you would want if your input were

my $data = [ { Ad1 => '20 SOUTH CENTRAL #B3', status => 'Property', City => 'NY' +, zCode => '0002', name => 'John D' }, { Ad1 => '15 SOUTH CENTRAL #B4', status => 'Property', City => 'NY' +, zCode => '0002', name => 'John V' }, { Ad1 => '100 main St.', status => 'houses', City => 'BO' +, zCode => '0007', name => 'Mary' }, { Ad1 => '13 Terminal St.', status => 'houses', City => 'LA' +, zCode => '6664', name => 'Larry' }, ];


Give a man a fish:  <%-{-{-{-<


In reply to Re^3: Add new data to array by AnomalousMonk
in thread Add new data to array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-19 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found