Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

how to dereference an Array of Hashes

by gepebril69 (Scribe)
on Oct 09, 2013 at 19:20 UTC ( [id://1057609]=perlquestion: print w/replies, xml ) Need Help??

gepebril69 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there

I work with an array of hashes to store values obtained out of an csv (comma separated value file). This works ok, but I like to use it in a subroutine with a reference to the original Array of Hashes. So I don't have to copy all the content all the time "moving" it from subroutine to subroutine. How should the grammar be?

sub extract_data_from_csv_line { my $Line = $_[0]; my $DataAoHRef = $_[1]; my @Content; my $i; my @Content = split(/,/, $Line); push ${$DataAoHRef}, { FirstName => $Content[0] , FamName => $Content +[1], Age => $Content[2] }; return(); } my $Line = "John,Malcovitz,35"; extract_data_from_csv_line($Line, \@$Line);

that doesn't seem to do the trick. What is the correct code for pushing with references?

Replies are listed 'Best First'.
Re: how to dereference an Array of Hashes
by davido (Cardinal) on Oct 09, 2013 at 19:34 UTC

    push @{$DataAoHRef}, ...

    But this is only a solution to your specific problem. The bigger problem is that you need to parse a CSV file. We all love to write code, but this problem is deceptive; it's harder to do right than one might suspect. And it's already been solved with Text::CSV.

    If you're planning on improving on what Text::CSV provides, that's great. If not, just use what's already available. That's why you're using Perl anyway, right? :)


    Dave

      Hi Dave,

      Thanks for the help. That is the line of code I was looking for

      I just need to read the csv info, from an xls file (I do this with xls2csv) and than read all lines so I can update a DB

      As far as I know I only need to go line by line to update the local DB

      I will have a look at this Perl module you proposed. Maybe it can provide me with new ideas.

Re: how to dereference an Array of Hashes
by 2teez (Vicar) on Oct 09, 2013 at 19:42 UTC

    check perldsc also.

    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://1057609]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found