Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: hash from CSV-like structure

by naChoZ (Curate)
on Jan 05, 2008 at 00:31 UTC ( #660490=note: print w/replies, xml ) Need Help??

in reply to hash from CSV-like structure

I wrote a subroutine to do almost the exact same thing just recently (though I ended up converting it to import to a db). Here it is with my db related stuff commented and fixed to populate a hashref like you've described with some sample usage based on your example.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Parse::CSV; my @list_of_fields = qw/ field1 field2 field3 field4 field5 field6 /; my $values = import_csv({ filename => '/tmp/vals.csv', fields => \@list_of_fields }); print "CSV Import complete...\n"; print Dumper( $values ); # {{{ import_csv # sub import_csv { my $args = shift; # die "No database handle provided for import...\n" unless defined +$args->{dbh}; # die "No database table provided for import...\n" unless defined +$args->{table}; die "No database columns provided for import...\n" unless defined +$args->{fields}; # my $dbh = $args->{dbh}; my $table = ref $args->{table} eq 'ARRAY' ? $args->{table}->[0] : $args->{table} ; my $fields = $args->{fields} ? $args->{fields} : 'auto'; my $csv = Parse::CSV->new( file => $args->{filename}, fields => $fields, sep_char => '|', ); my $results = {}; while ( my $row = $csv->fetch ) { my @columns = @{ $fields }; # Make certain that the number of values returned is # equal to the number of columns we're expecting. # die "Invalid number of columns...\n" unless scalar @columns == + scalar keys %{ $row }; # Creating placeholders this way so we'll always # have the exact right number of placeholders to # match the number of values in the columns list for # our sql statement. # # my @placeholders = map { '?' } @$columns; # my @values = map { $row->{$_} } sort @columns; # my $results = insert_row({ dbh => $dbh, # columns => \@columns, # table => $table, # 'values' => \@values, # }); $results->{ $row->{field1} }->{ $row->{field3} } = $row->{fiel +d5}; } return $results; } # }}}


# perl /tmp/ CSV Import complete... $VAR1 = { '200326951' => { 'rel_Access2' => '200315786', 'rel_Access1' => '200315786' } };


Therapy is expensive. Popping bubble wrap is cheap. You choose.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://660490]
[LanX]: memories....I missed my connection while chatting
[Discipulus]: in this case Corion we are speaking about software licensing: evry year or two we must rescan the whole ced to produce an excel report, while at every activation / disactivation we update a black box DB: i said that i a week i can produce the perl to..
[Discipulus]: rend out the xls IF i have access to the DB
[choroba]: LanX I miss working in a bank sometimes...
[Corion]: Discipulus: Ooof. Especially yearly things are things I like to automate instead of trying to remember how I did things last year...
[Corion]: And the second rule that I've learned is, that there is no one-off job, so writing a program for it pays off almost immediately. The third rule is to give all my programs numbers and have them reproduce that number in the name of their output files. :)
[Discipulus]: the true part is that also specification change between years.. but well our job is cheap but dont abuse of us.. ;=)
[LanX]: Choroba: do you miss chaos with ties? apply at the US government.. ;)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2017-03-29 12:10 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.