Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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

by kcott (Chancellor)
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1038417]
[Corion]: This is a toy clone of Jekyll, currently named MrHyde. But maybe I'll change that name to Jokel / Yokel, or some other play on the name , still mildly unfunny ;)
[choroba]: Or maybe Utterson?

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2018-05-22 11:15 GMT
Find Nodes?
    Voting Booth?