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

Re: Issues with Array of Hashes

by Lotus1 (Priest)
on Jan 20, 2017 at 18:40 UTC ( #1180046=note: print w/replies, xml ) Need Help??

in reply to Issues with Array of Hashes

Looking at the source code for the create method in Text::CSV::Slurp I see the die message you reported at the beginning.

sub create { my ( undef, %arg ) = @_; die "Need an an array of hashes input to create CSV from" unless exists $arg{input} && ref( $arg{input} ) eq 'ARRAY' && ref( @{ $arg{input} }[0] ) eq 'HASH';

One of those three things must be wrong with the array of hashes you are passing. You'll need to test your Arrays of Hashes before calling create. To start you could set up some tests to tell you when it fails. Something like:

use strict; use warnings; use Data::Dumper; sub generate { my %inline; $inline{'total'} = 2; $inline{'items'} = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; return \%inline; } my $ref = &generate(); print Dumper( $ref->{'items'} ); die "failed exists at $ref->{'total'}" unless exists $ref->{'items'}; die "not ARRAY at $ref->{'total'}" unless ref( $ref->{'items'} ) eq 'ARRAY'; die "not HASH at $ref->{'total'}" unless ref( @{ $ref->{'items'} }[0] ) eq 'HASH'; print "ok"; #my $csv = Text::CSV::Slurp->create( input => $ref->{'items'} ); __DATA__ $VAR1 = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; ok

Replies are listed 'Best First'.
Re^2: Issues with Array of Hashes
by ciscomonkey (Novice) on Jan 20, 2017 at 18:46 UTC
    Using that code, it makes it all the way through:
    $VAR1 = [ { 'name' => 'item1' }, { 'name' => 'item2' } ]; ok

      Yes it ran fine for me, as I showed with the ok at the end of my code. Also, your original code ran fine for me. What version of the modules are you using?

        Looks to be 1.03.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1180046]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-07-21 00:39 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (442 votes). Check out past polls.