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 | |
by Laurent_R (Canon) on May 01, 2014 at 13:49 UTC | |
by Anonymous Monk on May 01, 2014 at 12:07 UTC |