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

Re: Perl-ish way to create hash from array

by PrakashK (Pilgrim)
on Jul 09, 2013 at 20:58 UTC ( #1043376=note: print w/ replies, xml ) Need Help??


in reply to Perl-ish way to create hash from array

moritz and tangent have already answered your question. But, I would like to point out something in your code:

Here, you are iterating through the list of column names and building a new list in @csvColumns array.

# figure out the column headings from the first line my @csvColumns = (); foreach my $colName (@{$csvParser->getline($csvHandle)}) { push @csvColumns, $colName; }

In your foreach loop, you are building a (temporary) list by dereferencing the return value of the getline method (@{$csvParser->getline($csvHandle)}), then iterating through this list and copying each element into the other list (push @csvColumns, $colName;), and finally discarding (implicitly) the temporary list.

You could instead do:

# figure out the column headings from the first line my @csvColumns = @{$csvParser->getline($csvHandle)};


Comment on Re: Perl-ish way to create hash from array
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043376]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2015-07-06 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (71 votes), past polls