Perl哈希唯一性问题

时间:2013-09-29 08:28:52

标签: perl hash

我对perl非常缺乏经验,而且我需要完成一项简单的任务。我有一个字符串列表,其中字符串的第一部分是文件名,字符串的第二部分是版本号,由下划线分隔。我想打印出包含每个文件的最新版本的字符串。

例如,使用以下数据集:

P0689.D130624.C01_0
P0689.D130624.C02_0
P0689.D130624.C04_0
P0689.D130624.C05_0
P0730.D130624.C01_0
P0730.D130624.C01_1
P0730.D130624.C01_2
P0730.D130624.C01_0
P0730.D130624.C01_1

我的输出应该是:

P0689.D130624.C01_0
P0689.D130624.C02_0
P0689.D130624.C04_0
P0689.D130624.C05_0
P0730.D130624.C01_2

我认为最简单的方法是将文件名作为地图的键分开,然后插入版本作为值,如果它是当前的最大值。然后我会迭代哈希,并重新组合键/值对将给我我的结果。但是,每当我迭代哈希时,密钥似乎都不是唯一的。结果最终成为我的整个初始数据集,因为版本始终按升序排列。这可能是我对perl语法缺乏经验的结果,但非常感谢帮助。

这是我目前的代码:

my %hash = ();
foreach my $line (@F) {
    my @splt = split('_', $line);
    my $key = $splt[0];
    my $val = $splt[1];
    unless (exists $hash{$key} and $hash{$key} >= $val) {
        $hash{$key} = $val;
    }
};
while(my ($k,$v) = each %hash ) {
    print $k . '_' . $v, "\n";
};

编辑(2):有关输出数据的更多详细信息。

使用此脚本演示:

#!/bin/bash

DIR="/app/jobsandservices/shareddata/nphdocp1/2013/06/24/"
FILTER="[Pp]\(0689\|1391\|0730\)"


ls -1 $DIR*
echo -----
ls -1 $DIR* | grep "$FILTER"
echo -----
ls -1 $DIR* | grep "$FILTER" |  perl -an latest-version.pl

这是终端的EXACT输出:

/app/jobsandservices/shareddata/nphdocp1/2013/06/24/CONFIRM.C0668.S01125_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/CONFIRM.C0668.S26599_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0188.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0212.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0701.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0702.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0706.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0706.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0709.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_1
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_2
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0790.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0790.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0790.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0821.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0822.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0823.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0824.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0825.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0826.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0827.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2026.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2029.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2070.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2084.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2156.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2172.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2193.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2202.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2203.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C20_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C21_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C22_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C23_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2361.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2362.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2363.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2363.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2364.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2365.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2429.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2518.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C20_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C21_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C22_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C23_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C24_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2656.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C06_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C07_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C08_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C09_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C10_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C11_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C12_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C13_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C14_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C15_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C16_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C17_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C18_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C19_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C20_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C21_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C22_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C23_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C24_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2657.D130624.C98_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2658.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2659.D130624.C00_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P2660.D130624.C00_0
-----
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_1
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_2
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C02_0
-----
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C04_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0689.D130624.C05_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_1
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C01_2
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C02_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P0730.D130624.C03_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C01_0
/app/jobsandservices/shareddata/nphdocp1/2013/06/24/P1391.D130624.C02_0

这也是使用perl v5.10.1,因为可能存在环境问题。

1 个答案:

答案 0 :(得分:1)

你的代码很好。它只返回包含每个文件的最新版本的字符串。我认为你只是对输入数据感到困惑,因为最后两行

P0730.D130624.C01_0
P0730.D130624.C01_1

是重复的。这意味着对于集合{P,D,C} = {0730,130624,01},X = 0和X = 1并不是唯一的。 因此,您想要的输出不应包含最后一行

P0730.D130624.C01_1

此外,请注意使用while您无法对哈希进行排序以获得所需的输出。