如何在不使用中间变量的情况下将哈希转换为数组?

huangapple go评论56阅读模式
英文:

How to convert hash to array without the use of intermediate variable?

问题

如何将哈希转换为数组而不使用中间变量

例如,在以下代码中,"@arr" 变量是存储转换后的哈希的数组。

my %scientists = (
    "Newton"   => "Isaac",
    "Einstein" => "Albert",
    "Darwin"   => "Charles",
);

my @arr = %scientists;
print $_ . " " foreach @arr; # "Newton Isaac Darwin Charles Einstein Albert"

我这里没有尝试具体完成任何任务,只是想知道是否有可能将哈希转换为数组而不使用中间变量。

类似地,是否有办法可以替代"",以便以下操作成为可能:

print ref(<syntax>%scientists%<syntax>); # 应该打印数组。
英文:

How to convert hash to to array temporarily without using intermediate variable

For example in the following code "@arr" variable is an array storing the converted hash.

my %scientists = (
	&quot;Newton&quot;   =&gt; &quot;Isaac&quot;,
	&quot;Einstein&quot; =&gt; &quot;Albert&quot;,
	&quot;Darwin&quot;   =&gt; &quot;Charles&quot;,
);

my @arr = %scientists;
print $_ . &quot; &quot; foreach @arr; # &quot;Newton Isaac Darwin Charles Einstein Albert&quot;

I am not trying to accomplish anything specific here. Just want to know if its possible to convert hash to an array wihtout intermediate variable

print ref(\@arr); # print array

similarly is there a something that can replace "<syntax>" so that the following is possible

print ref(&lt;syntax&gt;%scientists%&lt;sytax&gt;) # should print array.

答案1

得分: 6

在列表上下文中,%hash 会导致标量的键-值对。

例如,%scientists 会产生六个标量 &quot;Newton&quot;, &quot;Isaac&quot;, &quot;Einstein&quot;, &quot;Albert&quot;, &quot;Darwin&quot;, &quot;Charles&quot;,尽管顺序可能会变化。

这适用于 my @arr = 的右侧。

my @arr = %scientists;

它还适用于 foreach 循环的列表。

print $_ . &quot; &quot; for %scientists;

然而,大多数情况下,人们会使用 keys( %scientists ) 而不仅仅是 %scientists

say &quot;$_ $scientists{ $_ }&quot; for keys( %scientists );

对于底部的问题,你可以使用

say ref( [ %scientists ] );

[ ... ] 大致相当于 do { my @anon = ...; \@anon }

换句话说,它仍然创建一个数组,但该数组是匿名的。

英文:

In list context, %hash results in a key-value pairs of scalars.

For example, %scientists results in the six scalars &quot;Newton&quot;, &quot;Isaac&quot;, &quot;Einstein&quot;, &quot;Albert&quot;, &quot;Darwin&quot;, &quot;Charles&quot;, although the order can vary.

This applies on the right-hand side of my @arr =.

my @arr = %scientists;

And it also applies for the list of a foreach loop.

print $_ . &quot; &quot; for %scientists;

Most of the time, however, one would use keys( %scientists ) instead of just %scientists.

say &quot;$_ $scientists{ $_ }&quot; for keys( %scientists );

For the question at the bottom, you could use

say ref( [ %scientists ] );

[ ... ] is roughly equivalent to do { my @anon = ...; \@anon }.

In other words, it still creates an array, but the array is anonymous.

答案2

得分: 3

以下是翻译好的部分:

问题的措辞似乎不一致 - 它似乎是在问一个数组,实际上想要一个列表,但讨论并不清楚支持这一点。也许这是一个关于如何“序列化”或字符串化哈希的问题,而不创建变量。以下是一些选项:

我经常使用以下方式打印哈希表:

say "$_ => $h{$_}" for keys %h; 

或者,为了始终按相同顺序显示,

say "$_ => $h{$_}" for sort keys %h; 

这种方法可以用来构建键值对的列表或字符串。例如

map { "$_ => $h{$_}" } keys %h;   # 键值对字符串的列表

可以进一步进行操作。 (使用哈希引用时是 keys %$hr$h->{$_})。

此外,“dumper”库可以为我们解压数据结构

use Data::Dump qw(pp);

my $fmt_str = pp \%h;  # 格式化字符串。用于格式化打印的话使用 'dd'

需要安装Data::Dump包。一个核心模块,应该与Perl一起安装的是Data::Dumper(函数 Dumper 返回一个格式化的字符串)。当然还有其他方法。

英文:

The wording of the question seems inconsistent -- it is asking about an array, seemingly wanting a list in fact, but the discussion doesn't clearly support that either. Perhaps it is a quest of how to "serialize" or stringify a hash, without creating variables. Here are some options

To print a hash I often use

say &quot;$_ =&gt; $h{$_}&quot; for keys %h; 

or, so that it is always in the same order,

say &quot;$_ =&gt; $h{$_}&quot; for sort keys %h; 

This approach can be used to build a list or a string of the key-value pairs. For example

map { &quot;$_ =&gt; $h{$_}&quot; } keys %h;   # list of key-value strings 

what can be further manipulated. (With a hashref it's keys %$hr and $h-&gt;{$_} instead.)

Also, "dumper" libraries unpack data structures for us

use Data::Dump qw(pp);

my $fmt_str = pp \%h;  # formatted string. for formatted print use &#39;dd&#39;

The package Data::Dump need be installed. A core module, which should come installed with Perl, is Data::Dumper (function Dumper returns a formatted string). There are yet others .

答案3

得分: 0

你想要转换做什么?

如果你只想要键,你可以这样做:

my @lastnames = keys %scientists;

如果你只想要值,你可以这样做:

my @firstnames = values %scientists;

如果你想要它们混合在一起作为键/值对,只需这样做:

my @names = %scientists;

需要记住的是,由于哈希是无序的,你不知道会以什么顺序返回。

英文:

What do you want the conversion to do?

If you want only the keys you can do

my @lastnames = keys %scientists;

If you want only the values you can do

my @firstnames = values %scientists;

If you want them intermingled as key/value pairs just do

my @names = %scientists;

The thing to remember is that since hashes are unordered, you don't know what order you will get back.

答案4

得分: 0

I understand your instructions. Here is the translated content:

你的措辞有点混淆(Array vs List),但我认为你真正想要的是将 %Hash 变量转换为 List,以便在没有中间 @array 变量的情况下进行 dump 操作。

以下是在调试器中的快速演示:

> print ref(@arr); # 打印数组

  DB&lt;7&gt; use Data::Dumper

  DB&lt;8&gt; print Dumper \%h
$VAR1 = {
          &#39;Einstein&#39; =&gt; &#39;Albert&#39;,
          &#39;Darwin&#39; =&gt; &#39;Charles&#39;,
          &#39;Newton&#39; =&gt; &#39;Isaac&#39;
        };

  DB&lt;9&gt;

注意:Data::Dumper 是 Perl 的核心模块,即在每个 Perl 版本中都可用。

我个人更喜欢 Data::Dump,它需要从 CPAN 安装。

英文:

Your wording is a bit confusing (Array vs List), but I think what you really want is to convert a %Hash variable to a List in order to dump it, without intermediate @array variable.

Here a quick demo in the debugger:

> print ref(@arr); # print array

  DB&lt;7&gt; use Data::Dumper

  DB&lt;8&gt; print Dumper \%h
$VAR1 = {
          &#39;Einstein&#39; =&gt; &#39;Albert&#39;,
          &#39;Darwin&#39; =&gt; &#39;Charles&#39;,
          &#39;Newton&#39; =&gt; &#39;Isaac&#39;
        };

  DB&lt;9&gt;

NB: Data::Dumper is core, i.e. available with every Perl version.

I personally prefer Data::Dump, which needs to be installed from CPAN.

huangapple
  • 本文由 发表于 2023年4月6日 21:51:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定