On Thu, Aug 6, 2009 at 3:36 PM, Nick Brown <nickbr...@gmail.com> wrote:

> Sorry for the late reply.
>
> I actually switched to using:
>
> use Finance::OFX::Parse::Simple;
>
> http://search.cpan.org/dist/Finance-OFX-Parse-Simple/lib/Finance/OFX/Parse/Simple.pm
>
> and this does appear to parse the data.
>
>    my $ofx_parser = Finance::OFX::Parse::Simple->new;
>    my $ofx_data   = $ofx_parser->parse_scalar($response->content);
>
>    print Dumper $ofx_data;
>    print ${$ofx_data}[0]->account_id;
>
> The print Dumper line seems to output the OFX data just fine.
> But due to my inexperience at perl the next line to print the
> account_id fails with following error:
>
> Can't call method "account_id" on unblessed reference at
> download_statements.pl line 72, <> line 1.


Well I don't really know enough about the context here, but my guess would
be that:

 $ofx_data  # is a reference to a list of hash references
 ${ $ofx_data }[0]   # returns the first hash reference
 $ofx_data->[0] # returns the first hash reference as well
 account_id   # is a key to these hashes

Assuming that is true, to get the account_id in the first hash I would
write:

 $ofx_data->[0]{account_id};

The arrow (->) operator is used (at least) two ways in Perl.  It can be used
to call a method on a class or instance, like $listObject->sort(); , or it
can be used to dereference and specify a location in the array when used
with an array reference, or to dereference and specify a key-value lookup
when used with a hash reference.  After the arrow, a value in [] means it's
an array reference and subscript, and a string in {} means it's a hash
reference and key.  So

 $ofx_data->[0]->{account_id}; # gets the account id
 $ofx_data->[0]{account_id}; # same thing - perl lets you omit the arrow
between multiple subscripts
 $ofx_data->[0]{ 'account_id' }; # same thing - perl lets you omit the
quotes in simple key names

Here's an example of how you could loop through and print out all the keys
and values given a list reference to a list of hash references.  It prints
out:

$ perl listOfHashes.pl
able to access account_id

Print them all:
 account_id: 1002972
 wishful_balance: $1,238,719.28
 fruit: strawberries
 vegetable: broccoli
 write_on: pad of paper
 write_with: pencil

And here's the code:

#!/usr/bin/perl
use strict;use warnings;
my $moneyHash = {    account_id => 1002972,    wishful_balance =>
'$1,238,719.28',};
my $foodHash = {    fruit => 'strawberries',    vegetable => 'broccoli',};
my $officeHash = {    write_with => 'pencil',    write_on => 'pad of paper',};
my $listRef = [$moneyHash, $foodHash, $officeHash];
# test hash dereferenceif ($listRef->[0]{account_id} == 1002972) {
print "able to access account_id\n";}
# print them allprint "\nPrint them all:\n";foreach my $hashRef
(@$listRef) {    foreach my $key (sort keys %$hashRef) {        print
" $key: ", $hashRef->{$key}, "\n";    }}


- John

Reply via email to