追加到Bash中的哈希表 [英] Appending to a hash table in Bash

查看:189
本文介绍了追加到Bash中的哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用目录中的所有文件的文件名和另一个整数形成哈希表。像文件名:数字。代码应该在bash 4.x.这是我写的代码:

 #!/ bin / bash 
DIR =`ls`
声明-A ARRAY
ZERO = 0
在$ DIR中的文件
do
echo我们有$ FILES
ARRAY + =([$ FILES] =$ ZERO)
done
echo完成填充数组!
在$ {ARRAY [@]}中的文件;
KEY =$ {file %%:*}
VALUE =$ {file ## *:}
printf%s has number%s.\\\
$ KEY$ VALUE
done
echo我们在这里完成了!
echo检查:
printf%s的数字为%s\\\
$ {ARRAY [1] %%:*}$ {ARRAY [1] ## *

,它给出了这个输出:

  [Anil @ computer68 test] $ bash kenzo.sh 
bash kenzo.sh
我们有dafuq.sh
我们有hello.cpp
我们有kenzo.sh
完成填充数组!
0的数字为0.
0的数字为0.
0的数字为0.
我们在这里完成!
支票:
的号码是

我该如何解决?哈希表中的任何元素都不具有文件名和数字的任何值。

解决方案

在您的情况下,您只能收到数组中的值始终为0。要获取关联数组的键,请在$ {!ARRAY [@]}中使用

 

(注意的关联数组)。您可以使用 $ {ARRAY [$ file]} 符号打印数组的值。与这里的正常数组相反,需要 $ ,否则返回文件。无论如何,这里返回的值将始终为0。



所以第二个循环可能是:



<$ p $ $ {!ARRAY [@]}中的KEY为p> ; do
VALUE =$ {ARRAY [$ KEY]}
printf%s has number%s.\\\
$ KEY$ VALUE
done

评论




  • 我没有看到在这里使用关联数组。简单的数组应该做。

  • 为什么要剪切字符?我看到它不是文件名的一部分。也许您认为关联数组的元素位于 KEY:VALUE 表单中。不是这样。 ARRAY 的索引是 KEY ,值为 VALUE

  • 而不是 DIR =`ls` 和$ code>在$ DIR中的文件; ... 你可以在*中使用的文件; ...

  • 这是正确的: ARRAY + =([$ FILES] =$ ZERO),但您也可以使用 ARRAY [$ FILES] =$ ZERO表单。这是一个更有说服力的一点。


I am trying to make a hash table with the file name of all files in a directory and another integer number. Like "File Name" : "number". The code should be in bash 4.x. This is the code which I wrote:

#!/bin/bash
DIR=`ls`
declare -A ARRAY
ZERO=0
for FILES in $DIR
do
    echo "We have $FILES"
    ARRAY+=(["$FILES"]="$ZERO")
done
echo "Done with filling up array!"
for file in "${ARRAY[@]}" ; do
    KEY="${file%%:*}"
    VALUE="${file##*:}"
    printf "%s has number %s.\n" "$KEY" "$VALUE"
done
echo "We are done here!"
echo "Check: "
printf "%s has the number %s\n" "${ARRAY[1]%%:*}" "${ARRAY[1]##*:}"

and it gives this output:

[Anil@computer68 test]$ bash kenzo.sh 
bash kenzo.sh 
We have dafuq.sh
We have hello.cpp
We have kenzo.sh
Done with filling up array!
0 has number 0.
0 has number 0.
0 has number 0.
We are done here!
Check: 
 has the number 

How do I fix it? None of the elements in the hash table have any values of the file name and the number.

解决方案

In your case you receives only the values from the array, which are always '0'. To get the keys of an associative array, use

for file in "${!ARRAY[@]}" ; do

(Mind the ! before the name of the associative array). You can print the value of the array using the ${ARRAY[$file]} notation. Contrary to normal arrays here the $ is needed, otherwise it returns the file indexed. Anyway here the returned value will be always '0'.

So the second loop could be:

for KEY in "${!ARRAY[@]}" ; do
    VALUE="${ARRAY[$KEY]}"
    printf "%s has number %s.\n" "$KEY" "$VALUE"
done

Comments

  • I see no point to use associative arrays here. Simple array should do.
  • Why do you cut the : character? As I see it is not part of the file names. Maybe You think that the element of an associative array is in KEY:VALUE form. It is not. The index of the ARRAY is KEY and the value is VALUE.
  • Instead of DIR=`ls` and for FILES in $DIR;... you could use for FILES in *; ....
  • This is correct: ARRAY+=(["$FILES"]="$ZERO"), but you can use ARRAY[$FILES]="$ZERO" form as well. It is a little bit more talkative.

这篇关于追加到Bash中的哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆