string - 如果两个键相同但值不同,谁能解释如何打印所有键?
问题描述
在此代码中,两个键“39”名称相同但值不同,我想打印两个键
use strict;
use warnings;
my %studentnames = (
14 => Martha,
27 =>Vivek,
31 =>Era,
16 =>Marty,
25 =>Jason,
29 =>Socrates,
19 =>Uri,
39 =>Nitin ,
39 =>Plato,
);
foreach my $name (sort keys %studentnames)
{
printf "%-8s %s\n", $name, $studentnames{$name};
}
我收到错误。
Bareword "Martha" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Vivek" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Era" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Marty" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Jason" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Socrates" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Uri" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Nitin" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
Bareword "Plato" not allowed while "strict subs" in use at /home/9945b48d30946ed2641d9778b42cb182.pl line 10.
预期产出
14 Martha
27 Vivek
31 Era
16 Marty
25 Jason
29 Socrates
19 Uri
39 Nitin
39 Plato
谁能告诉我该怎么做?
解决方案
两个键不能相同。一个会覆盖另一个。如果你想为一个键设置多个值,那么你需要设计你的数据结构来支持它(例如,通过让值是一个数组引用)。
您的错误消息与该问题无关(您忘记在字符串值周围加上引号)。
推荐阅读
- macos - 当 Application 退出时,NSDocument 的 isDocumentEdited 方法总是返回 false
- php - 每次使用相同的浏览器信息进行 Laravel Dusk 测试
- postgresql - 使用持久卷启动 postgres 容器 [docker compose]
- url - Solr 查询 - 当我请求具有大量方面的搜索时,URL 太长
- angular - angular 4 - 如何以垂直方式显示 json 数组
- python - 使用 Python 和 SQLAlchemy 处理复杂的 SQL 语句
- json - Springboot - 将 JSON 转换为 JAVA
- java - Docker maven 映像正在创建 root 拥有的目标文件夹,Jenkins 无法将其从其工作区中删除
- sql - SQL 3 ID 随时间变化
- java - 从 TableView 中的按钮列中删除行 [Java]