-3

我对 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

这是终端的确切输出:

/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,因为可能存在环境问题。

4

1 回答 1

1

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

P0730.D130624.C01_0
P0730.D130624.C01_1

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

P0730.D130624.C01_1

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

于 2013-09-29T08:48:49.437 回答