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

Re: .csv file in to hash of hashes for a Berkley DB (for a hash newbie)

by kcott (Abbot)
on Jun 12, 2013 at 09:20 UTC ( #1038417=note: print w/ replies, xml ) Need Help??


in reply to .csv file in to hash of hashes for a Berkley DB (for a hash newbie)

G'day Doozer,

"I don't think I am adding the new details for each box to the hash correctly."

That's not your problem (although there's probably a better way to achieve this). See DB_File - "How do I store complex data structures with DB_File?":

Although DB_File cannot do this directly, there is a module which can layer transparently over DB_File to accomplish this feat.

Check out the MLDBM module, available on CPAN in the directory modules/by-module/MLDBM.

The value you're associating with each $hash{$location} is the stringified representation of the hashref { 'Date Active' => $date_active, ... } (i.e. HASH(0xhex-digits)) — this is the string you can't use as a hash reference.

Regarding my comment "there's probably a better way to achieve this", consider using a hash slice (see perldata - Slices) instead of assigning each element of @box to a separate variable which is subsequently used just once as a value. Here's an example using a cutdown version of your code and data:

$ perl -Mstrict -Mwarnings -E ' my @STB_keys = ("Date Active", "Manufacturer"); my %hash; my $line = "Zone1 - Box1,01/06/2011,Amstrad"; my @box = split /,/ => $line; my $location = shift @box; @{$hash{$location}}{@STB_keys} = @box; say $hash{"Zone1 - Box1"}{"Date Active"}; say $hash{"Zone1 - Box1"}{"Manufacturer"}; ' 01/06/2011 Amstrad

Also, splitting a CSV file on a comma will break if any value contains a comma: Text::CSV is a better choice.

-- Ken


Comment on Re: .csv file in to hash of hashes for a Berkley DB (for a hash newbie)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (13)
As of 2015-07-31 18:23 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 (280 votes), past polls