perl - How to print an array that it looks like a hash -



perl - How to print an array that it looks like a hash -

this question has reply here:

i need help in perl, how write code output of csv file in form of hash [closed] 1 reply

i new perl, , have write code takes contents of file , array , print output looks hash. here illustration entry:

my %amino_acids = (f => ["phenylalanine", "phe", ["ttt", "ttc"]])

out set should in above format.

lines of files this...

"methionine":"met":"m":"aug":"atg" "phenylalanine":"phe":"f":"uuu, uuc":"ttt, ttc" "proline":"pro":"p":"ccu, ccc, cca, ccg":"cct, ccc, cca, ccg"

i have take lastly codons after semicolon , ignore first group.

is intention build equivalent hash? or really want string format? programme uses text::csv build hash file , dumps using data::dump have string format well.

use strict; utilize warnings; utilize text::csv; utilize data::dump 'dump'; $csv = text::csv->new({ sep_char => ':' }); open $fh, '<', 'amino.txt' or die $!; %amino_acids; while (my $data= $csv->getline($fh)) { $amino_acids{$data->[2]} = [ $data->[0], $data->[1], [ $data->[4] =~ /[a-z]+/g ] ]; } print '$amino_acids = ', dump \%amino_acids;

output

$amino_acids = { f => ["phenylalanine", "phe", ["ttt", "ttc"]], m => ["methionine", "met", ["atg"]], p => ["proline", "pro", ["cct", "ccc", "cca", "ccg"]], }

update

if really don't want install modules (it straightforward process , makes code much more concise , reliable) need.

use strict; utilize warnings; open $fh, '<', 'amino.txt' or die $!; print "my %amino_acids = (\n"; while (<$fh>) { chomp; @data = /[^:"]+/g; @codons = $data[4] =~ /[a-z]+/g; printf qq{ %s => ["%s", "%s", [%s]],\n}, @data[2,0,1], bring together ', ', map qq{"$_"}, @codons; } print ")\n";

output

my %amino_acids = ( m => ["methionine", "met", ["atg"]], f => ["phenylalanine", "phe", ["ttt", "ttc"]], p => ["proline", "pro", ["cct", "ccc", "cca", "ccg"]], )

perl

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -