2

I have generated array of hash in the following format using Text::CSV_XS module

Output of Data Dumper:

$VAR1 = [
    {   'TITLE'       => 'test csv',
        'SEVERITY'    => '3',
        'Attachments' => 'dklsfj/dksfj.dskak/fsajk',
        'ID'          => '123',
        'STATUS'      => 'pending',
        'History'     => 'repeat',
        'priority'    => '4',
        'DESCRIPTION' => 'fdlkfjalskfjlskflafkdalsfjkasljfkldksajdfklsajkl',
        'PROJECT'     => 'hadkf'
    },
    {   'PROJECT'     => 'hadkf',
        'DESCRIPTION' => 'fdlkfjalskfjlskflafkdalsfjkasljfkldksajdfklsajkl',
        'priority'    => '4',
        'History'     => 'repeat',
        'ID'          => '124',
        'STATUS'      => 'pending',
        'Attachments' => 'dklsfj/dksfj.dskak/fsajk',
        'SEVERITY'    => '3',
        'TITLE'       => 'test csv'
    },
    {   'Attachments' => 'dklsfj/dksfj.dskak/fsajk',
        'ID'          => '125',
        'STATUS'      => 'pending',
        'SEVERITY'    => '3',
        'PROJECT'     => 'hadkf',
        'History'     => 'repeat',
        'priority'    => '4',
        'DESCRIPTION' => 'fdlkfjalskfjlskflafkdalsfjkasljfkldksajdfklsajkl',
        'TITLE'       => 'test csv'
    },
    {   'TITLE'       => 'test csv',
        'DESCRIPTION' => 'fdlkfjalskfjlskflafkdalsfjkasljfkldksajdfklsajkl',
        'History'     => 'repeat',
        'priority'    => '4',
        'PROJECT'     => 'hadkf',
        'SEVERITY'    => '3',
        'STATUS'      => 'pending',
        'ID'          => '126',
        'Attachments' => 'dklsfj/dksfj.dskak/fsajk'
    }
];

I am trying to print ID of the first array element as below

print "$aoh[0]{ID}";

but it returns the following error:

Not a HASH reference at csv_parse.pl line 12

I am new to using this module.Please let me know what the mistake is?

My code for generating the Array of hash is

use strict;
use warnings;
use Text::CSV_XS qw( csv );

open( my $fh, "<", "test.csv" ) or die "cannot open the file $!";
my @aoh = csv( { in => $fh, headers => "auto" } );
print Dumper(@aoh);
close($fh);
print "$aoh[0]{ID}";

1 Answer 1

1

As it looks first element of @aoh is array reference, so either dereference what function returns,

my @aoh = @{ csv ({ in => $fh, headers => "auto" }) };

or

print "$aoh[0][0]{ID}";

or

my $aoh = csv ({ in => $fh, headers => "auto" });
print "$aoh->[0]{ID}";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.