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

Re: storing a file in 2d array

by clueless newbie (Curate)
on May 01, 2020 at 19:45 UTC ( [id://11116330] : note . print w/replies, xml ) Need Help??


in reply to storing a file in 2d array

Let's make use of DBI and DBD::CSV.

use strict; use warnings; use Data::Dumper; use DBI; use 5.01800; # Get a connect to the tables my $dbh=DBI->connect ("dbi:CSV:", undef, undef, { f_dir => "/Users/Desktop/", f_ext => ".txt/r", csv_sep_char => "\t", RaiseError => 1, }) or die "Cannot connect: $DBI::errstr"; eval { my $sth=$dbh->prepare( # We want a concatenation of ... from the file Gene.txt in + /Users/Desktop qq{select concat(ProteinName,'; ',MF1,'; ',MF2,'; ',MF3) a +s whatever from Gene} ); $sth->execute(); # get the names of the fields returned by the select my $field_aref=$sth->{NAME}; # and dump them ... #warn Data::Dumper->Dump([\$field_aref],[qw(*field_aref)]),' '; # Get the selection one row at a time while (my $value_aref=$sth->fetchrow_arrayref()) { # dump the values from the select #warn Data::Dumper->Dump([\$value_aref],[qw(*value_aref)]),' ' +; # For simplicity we will make a hash where the keys are the fi +eld names and the values are the values of those fields my %_h; @_h{@$field_aref}=@$value_aref; # dump the hash to confirm all is what we expect #warn Data::Dumper->Dump([\%_h],[qw(*_h)]),' '; # since everything looks reasonable ... say $_h{whatever}; }; }; $@ and die "SQL database error: $@";

which (at least for me on Windows 10) yields

d:\scratch>perl 11116298.t GH1; Growth factor activity; Growth hormone receptor binding; Hormone +activity POMC; G protein-coupled receptor binding; Hormone activity; Signaling +receptor binding THRAP3; ATP binding Source; Nuclear receptor transcription coactivator + activity; Phosphoprotein binding