用数组初始化哈希的键
我需要初始化哈希值的哈希值。我的方法如下,请问有更优雅的方法吗?
$biggest_word_size = 0;
foreach $sig (@signals)
{
$sigs->{$sig} = "None";
$biggest_word_size =
( $biggest_word_size > length($sig) ) ? $biggest_word_size : length($sig) ;
}
I need to initialize a hash of hashes. The way I do it is below, is there a more elegant way to do it?
$biggest_word_size = 0;
foreach $sig (@signals)
{
$sigs->{$sig} = "None";
$biggest_word_size =
( $biggest_word_size > length($sig) ) ? $biggest_word_size : length($sig) ;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
说实话,对于你想要的,这几乎是你想要的优雅。您可以用稍微更惯用的方式来做到这一点,但是为什么?它具有完美的可读性并且足够简洁
如果您的数组大小不太大,并且您不关心压缩最后一盎司的性能,您可以以稍微更惯用的方式分别执行这两个任务(在扫描数组两次的成本):
To be honest, for what you want, this is pretty much as elegant as you want. You can do it in a slightly more idiomatic way, but why? It's perfectly readable and concise enough
If the size of your array is not too large, and you don't care about squeezing every last ounce of performane, you can do those 2 tasks separately in a slightly more idiomatic way (at the cost of scanning the array twice):
好吧,如果忽略
$biggest_word_size
位,则可以从数组初始化 hashrefWell, if you ignore the
$biggest_word_size
bit, to initialise a hashref from an array can be我会使用一种排序来实现这一点。
I would use a sort to achieve this.