Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Adding rows of data

by Anonymous Monk
on Dec 02, 2014 at 15:38 UTC ( [id://1108973]=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 trying to add two rows of data on top of this data structure, it adds, but the data structure doesn’t seem to be right. can anyone suggest how better and the right way to get this right?
Here is the test code I am trying:
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $data = _test(); my $header_row; push @$header_row, ["City Name", "Last Name", "First Name", "Addrress +",], ["", "", "", "",]; unshift @{ $data }, @{ $header_row }; print Dumper $data; sub _test { my @data = [ { 'CITY' => 'San Frans', 'LAST_NAME' => 'Doe', 'FIRST_NAME' => 'Jonh', 'ADDRESS' => '100 Main Street', }, ]; return \@data; }

Thanks for Help!

Replies are listed 'Best First'.
Re: Adding rows of data
by atcroft (Abbot) on Dec 02, 2014 at 16:03 UTC

    The existing $data is a reference to an array containing an array hashes, and the data you are adding are arrays being added to the top-level array. This is why your data structure looks odd (see below).

    If you change the assignment in _test to the form my @data = ( ... );, I believe it will appear more as you expect. (It does, however, mean that the elements of the array differ in type.)

    Hope that helps.

    Update: 2014-12-02

    Based on that premise, then, changing the unshift() line in your code to unshift @{ $data }[0], @{ $header_row }; appears to have your desired effect (see below).

      That’s my problem, I can't change the data in _test, I am trying to change the data in  $header_row to match what is coming from _test.

        Well, then at least add a hash reference instead of two array references to match data structure from your _test() ...

        ... push @{ $header_row } , [ { 'City Name' => '' , ... } ] ; ...

        BTW|FWIW, your headers|key names are different in letter case & spellings.

Re: Adding rows of data
by 2teez (Vicar) on Dec 02, 2014 at 18:32 UTC

    Hi, In MHO, I would have suppose that you push, the data from $data variable into the variable $header_row instead the other way around, using unshift like you are doing. Something like so

    ... push @$header_row, @$data; print Dumper $header_row; ..
    And of course, it gives you the same output like you had previously.
    However, to have your desired out something like this works
    use strict; use warnings; use Data::Dumper; my $data = _test(); my $header_row; push @$header_row, [ "City Name", "Last Name", "First Name", "Addrress +", ], [ "", "", "", "", ]; my @values = qw(CITY LAST_NAME FIRST_NAME ADDRESS); push @{$header_row} => [ map { @{$_}{@values} } map { @{$_} } @{$data} + ]; print Dumper $header_row; sub _test { my @data = [ { 'CITY' => 'San Frans', 'LAST_NAME' => 'Doe', 'FIRST_NAME' => 'Jonh', 'ADDRESS' => '100 Main Street', }, ]; return \@data; }
    Output
    $VAR1 = [ [ 'City Name', 'Last Name', 'First Name', 'Addrress' ], [ '', '', '', '' ], [ 'San Frans', 'Doe', 'Jonh', '100 Main Street' ] ];
    Since, you said you cannot change the subroutine _test()

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 08:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found