in reply to How to add missing part in a Hash of Array

Here is one way:

#! perl use strict; use warnings; use Data::Dump; my %HoA_sequence = ( 67 => 'A', 68 => 'S', 77 => 'W', 78 => 'P', 79 => 'I', ); $HoA_sequence{$_} //= '-' for 67 .. 79; dd \%HoA_sequence;

Output:

21:40 >perl 904_SoPW.pl { 67 => "A", 68 => "S", 69 => "-", 70 => "-", 71 => "-", 72 => "-", 73 => "-", 74 => "-", 75 => "-", 76 => "-", 77 => "W", 78 => "P", 79 => "I", } 21:42 >

Note: the line $HoA_sequence{$_} //= '-' for 67 .. 79; could be written more verbosely as:

for my $protein (67 .. 79) { unless (exists $HoA_sequence{$protein}) { $HoA_sequence{$protein} = '-'; } }

which may be clearer if you’re not comfortable with Perl’s defined-or operator and statement modifiers.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: How to add missing part in a Hash of Array
by Anonymous Monk on May 01, 2014 at 11:52 UTC
    I have created the Hash of Array based on a tab separated file... Thank you for the suggestion, but the thing is that I can't know beforehand which is the range of missing positions... I was thinking of having a way to "check" that there is a gap in the positions array, but how?
      You might have read it too quickly, but Athanasius's solution does not rely in any way on finding the range of missing positions, but only on knowing the current lowest and highest valid (i.e. defined) positions in the hash (i.e. smallest and highest keys). It is quite easy to find them, using the min and max functions of the List::Utils module or rolling out your own algorithm to do it:
      my ($min, $max) = (1e10, -1e10); for my $value (keys %hash) { $min = $value if $value < $min; $max = $value if $value > $max; } # $now $min has the smallest key and $max the highest one
      Or, if your data is small, you could possibly even use the sort function to do it in one single instruction:
      my ($min, $max) = (sort {$a<=>$b} keys %hash)[0,-1];
      But that's getting quickly rather inefficient when the data is growing.

      Maybe this helps?

      use List::Util qw/min max/; # ... $HoA_sequence{$_} //= '-' for min(keys %HoA_sequence) .. max(keys %HoA_sequence);