How to apply splicing or any other function to several different arrays in Perl?

I am trying to shorten the following code:

    if ( /MATCH/ ){
        splice @identifiers,            $i, 1;
        splice @sequences,              $i, 1;
        splice @optional_informations,  $i, 1;
        splice @quality_scores,         $i, 1;
        splice @barcodes,               $i, 1;
    }

Is there a way to iterate over each array and perform a splice or any other function?

+4
source share
3 answers

You can iterate over an array of links:

@all_arrays = \( # Note the ref-making backslash applied to the list
    @identifiers,
    @sequences,
    @optional_informations,
    @quality_scores,
    @barcodes
);
for $array (@all_arrays)
{
    splice @$array, $i, 1;
}
+9
source

When you discover that you want to do similar things for a number of related data structures, this indicates that they should be members of a larger data structure. In this case, you can put all arrays in a hash:

my %dataset = (
    identifiers    => [],
    sequences      => [],
    optional_info  => [],
    quality_scores => [],
    barcodes       => [],
);

if ( /MATCH/ ) {
    splice @$_, $i, 1 for values %dataset;
}

, @ikegami , , . , - , , quality_scores 30 70, ( ) ( .

,

 my %dataset = (
    id0 => {
        t => '...',
        id => '...',
        sequence => '...',
        optional_info => '...',
        quality_score => '...',
        barcoode => '...',
    },
    # ...
    idn => {
        t => '...',
        id => '...',
        sequence => '...',
        optional_info => '...',
        quality_score => '...',
        barcoode => '...',
    },
 );

, ( , t).

+8

, .

foreach my $arr_ref (\(@identifiers,@sequences,@optional_informations,@quality_scores)){

     my @tmparr=@{ $arr_ref }[3,4];#slice operation or any other operation can be performed

}
0

Source: https://habr.com/ru/post/1676475/


All Articles