小编Nic*_*zer的帖子

perl哈希中自动递增数字键值?

我有一个perl脚本,我在其中读取给定目录中的文件,然后将这些文件放入一个数组中.然后,我希望能够将这些数组元素移动到perl哈希中,数组元素是哈希值,并自动为每个哈希值分配数字键.

这是代码:

    # Open the current users directory and get all the builds. If you can open the dir
    # then die.
    opendir(D, "$userBuildLocation") || die "Can't opedir $userBuildLocation: $!\n";
    # Put build files into an array.
    my @builds = readdir(D);
    closedir(D);
    print join("\n", @builds, "\n");
Run Code Online (Sandbox Code Playgroud)

打印出来:

    test.dlp
    test1.dlp
Run Code Online (Sandbox Code Playgroud)

我想获取这些值并将它们插入一个看起来像这样的哈希:

my %hash (
            1 => test.dlp
            2 => test1.dlp
         );
Run Code Online (Sandbox Code Playgroud)

我希望编号键根据我在给定目录中找到的文件数量自动递增.

我只是不确定如何将自动递增键设置为散列中每个项目的唯一数值.

arrays perl hash

3
推荐指数
2
解决办法
2502
查看次数

标签 统计

arrays ×1

hash ×1

perl ×1