如何将字符串拆分为具有 undef 值的哈希键?
这是我的字符串:NANA TEKA KAOE FLASK LSKK
我该如何制作它,使其看起来像这样:哈希 = {NANA => undef,TEKA => undef, KAOE => undef, ...
当然,我总是可以先将其拆分为一个数组
然后循环遍历每个值,然后将它们分配为散列
键...但是是否有更短/更简单的方法来做到这一点?
提前致谢!
Here's my string:NANA TEKA KAOE FLASK LSKK
How do I make it so that it'll look like this:HASH = {NANA => undef, TEKA => undef, KAOE => undef, ...
Of course I could always split this into an array first
then loop through each value then assign them as hash
keys... but If there's a shorter/simpler way to do that?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以拆分字符串并使用映射来生成输出哈希。
You can split the string and use a map to generate the output hash.
@hash{ split /\s+/, $string } = ();
@hash{ split /\s+/, $string } = ();
我怀疑这是否是最简洁的方法,但它似乎有效:
I doubt if this is the most succinct way to do it, but it seems to work: