从散列中的数组中删除值
Remove values from array inside a hash
我有一个散列 %m_h
,里面有几种不同的数据类型。我想从 $VAR4
中的数组中删除项目 'q20_bases'
,但不知道如何删除。
数据结构(来自print Dumper %m_h
)
$VAR1 = 'run_m';
$VAR2 = [
'run_id',
'machine',
'raw_clusters',
'passed_filter_reads',
'yield'
];
$VAR3 = 'ln_m';
$VAR4 = [
'run_id',
'lane_number',
'read_number',
'length',
'passed_filter_reads',
'percent_passed_filter_clusters',
'q20_bases',
'q30_bases',
'yield',
'raw_clusters',
'raw_clusters_sd',
'passed_filter_clusters_per_tile',
'passed_filter_clusters_per_tile_sd',
'percent_align',
'percent_align_sd'
];
我尝试了 delete $m_h{'q20_bases'};
,但它什么也没做,而且我不确定该朝哪个方向前进。
delete 从散列中删除键和关联值,而不是从数组中删除元素。
您可以使用 grep 来 select 与 q20_bases
不同的数组元素。
$m_h{ln_m} = [grep $_ ne 'q20_bases', @{ $m_h{ln_m} }];
或
@{ $m_h{ln_m} } = grep $_ ne 'q20_bases', @{ $m_h{ln_m} };
你也可以使用splice从数组中删除一个元素,但你需要知道它的索引:
my ($i) = grep $m_h{ln_m}[$_] eq 'q20_bases', 0 .. $#{ $m_h{ln_m} };
splice @{ $m_h{ln_m} }, $i, 1;
您可以看到,您总是需要使用 @{...}
取消引用值才能从数组引用中获取数组。最近的 Perls 也为它提供了另一种语法:
$m_h{ln_m}->@*
我有一个散列 %m_h
,里面有几种不同的数据类型。我想从 $VAR4
中的数组中删除项目 'q20_bases'
,但不知道如何删除。
数据结构(来自print Dumper %m_h
)
$VAR1 = 'run_m';
$VAR2 = [
'run_id',
'machine',
'raw_clusters',
'passed_filter_reads',
'yield'
];
$VAR3 = 'ln_m';
$VAR4 = [
'run_id',
'lane_number',
'read_number',
'length',
'passed_filter_reads',
'percent_passed_filter_clusters',
'q20_bases',
'q30_bases',
'yield',
'raw_clusters',
'raw_clusters_sd',
'passed_filter_clusters_per_tile',
'passed_filter_clusters_per_tile_sd',
'percent_align',
'percent_align_sd'
];
我尝试了 delete $m_h{'q20_bases'};
,但它什么也没做,而且我不确定该朝哪个方向前进。
delete 从散列中删除键和关联值,而不是从数组中删除元素。
您可以使用 grep 来 select 与 q20_bases
不同的数组元素。
$m_h{ln_m} = [grep $_ ne 'q20_bases', @{ $m_h{ln_m} }];
或
@{ $m_h{ln_m} } = grep $_ ne 'q20_bases', @{ $m_h{ln_m} };
你也可以使用splice从数组中删除一个元素,但你需要知道它的索引:
my ($i) = grep $m_h{ln_m}[$_] eq 'q20_bases', 0 .. $#{ $m_h{ln_m} };
splice @{ $m_h{ln_m} }, $i, 1;
您可以看到,您总是需要使用 @{...}
取消引用值才能从数组引用中获取数组。最近的 Perls 也为它提供了另一种语法:
$m_h{ln_m}->@*