Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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]
NodeReaper snacks on some Soylent Green

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2018-07-23 13:57 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (468 votes). Check out past polls.