sql - Perl:在数组的哈希中组合重复的键
问题描述
我对此有疑问,想知道是否有人可以提供帮助。我正在解析一个 .txt 文件,并且想要组合重复的键和它的值。本质上,对于每个标识符,我想存储它的高度值。每个“样本”有 2 个条目(A 和 B)。我有这样存储的文件:
while(...){
@data= split ("\t", $line);
$curr_identifier= $data[0];
$markername= $data[1];
$position1= $data[2];
$height= $data[4];
if ($line >0){
$result[0] = $markername;
$result[1] = $position1;
$result[2] = $height;
$result[3] = $curr_identifier;
$data{$curr_identifier}= [@result];
}
}
这似乎工作正常,但我的问题是当我将此数据发送到下面的函数时。它打印 $curr_identifier 两次。我只想填充唯一标识符并检查它的 $height 变量是否存在。
if (!defined $data{$curr_identifier}[2]){
$output1= "no height for both markers- failed";
} else {
if ($data{$curr_identifier}[2] eq " ") {
$output1 = $markername;
}
}
print $curr_identifier, $output1 . "\t" . $output1 . "\n";
基本上,如果两个标记 (A&B) 都存在样本高度,则输出是两个标记。
'1', 'A', 'B'
如果高度不存在,则报告标记的输出为空。
'2', 'A', ' '
'3', ' ', 'B'
我当前的输出是这样打印出来的:
1, A
1, B
2, A
2, ' '
3, ' '
3, B'
_DATA_
Name Marker Position1 Height Time
1 A A 6246 0.9706
1 B B 3237 0.9706
2 A 0
2 B B 5495 0.9775
3 A A 11254 0.9694
3 B 0
解决方案
您想要的输出基本上可以归结为这几行 perl 代码:
while (<DATA>) {
($name,$mark,$pos,$heig,$time) = split /\t/;
print "'$name','$mark','$pos'\n";
}
__DATA__
... your tab-separated data here ...
推荐阅读
- json - 无法访问此 JSON 嵌套对象
- android - FCM Token 第一次不生成,第二次生成
- javascript - 移除使用 bind(this) 添加的事件监听器
- c# - 验证动态对象中的属性
- ffmpeg - FFMPEG:如何在 centos 上启用 libvorbis 和 libvpx
- azureservicebus - 加快发送消息
- time-complexity - max char 子序列的时间复杂度
- ios - 分发时手动集成框架失败
- sql - Hibernate 在查询实体的子类型字段时生成 IN 运算符;使用条件 API 时导致正确查询生成问题
- linux - Windows sublinux - 获取调用 linux 的 Windows 路径