in reply to Adding missing values into a hash

Hi,

in your inner loop you have to replace the last line

my ($string, $int) = (split(/\;/, $dat));

by the following

my @elements = split /;/, $data; my %rowvalues; foreach my $element (@elements) { my ($key, $value) = split /=/, $element; $rowvalues{$key} = $value; } foreach my $key (keys %info) { if(exists $rowvalue{$key}) { push @{$info{$key}}, $rowvalue{$key}; } else { push @{$info{$key}}, 'NA'; } }

I hope that is it. I haven't tested. Please put code tags around your sample data so we can see the structure better.

Regards
McA

Replies are listed 'Best First'.
Re^2: Adding missing values into a hash
by Biopolete (Initiate) on Jun 19, 2014 at 14:33 UTC

    Thank you very much for your answer :)

    I was trying something similar, but the problem is that at the end I obtain a hash of hashes, I don't know why. And it's imposible working with them.

      Hi

      I was wondering about your answer and therefore made this selfcontained snippet which should show the relevant elements.

      #!/bin/env perl use strict; use warnings; use 5.010; my %info; while (my $line = <DATA>) { chomp $line; if ($line =~ /##INFO=<ID=/) { my ($first, $second) = split /,/, $line; my ($firstsecond, $secondsecond) = split /ID=/, $first; $info{$secondsecond}=(); } elsif ($line !~ /#/) { my ($numbers, $data) = split /\s+/, $line; foreach my $dat ($data){ my @elements = split /;/, $data; my %rowvalues; foreach my $element (@elements) { my ($key, $value) = split /=/, $element; $rowvalues{$key} = $value; } foreach my $key (keys %info) { if(exists $rowvalues{$key}) { push @{$info{$key}}, $rowvalues{$key}; } else { push @{$info{$key}}, 'NA'; } } } } else { next; } } foreach my $header (sort keys %info) { say $header, ' => ', join(',', @{$info{$header}}); } __DATA__ # First the headers ##INFO=<ID=AA, ##INFO=<ID=AB, ##INFO=<ID=AC, # then the data 1 AA=1;AB=2;AC=3 2 AA=2;AB=2 3 AA=5;AB=1;AC=1

      I hope this will clarify what was said before. I change one split from '\t' to '\s+' because of pasting this code herein would probably destroy tghe tab character.

      Regards
      McA

        Thank you very much. It works perfect :)