perl - 如何在 PERL 中打印数组引用的值?
问题描述
我在 Perl 中有一个带方括号的定义天数数组。我想访问数组的每个元素。下面代码中的一个类似示例(这只是一段代码):-
@days = [a,2,3];
foreach(@days){print "$_\n";}
print "\n\n @days";
输出为 ARRAY(0x2032950)
数组(0x2032950)
我需要访问数组 elementS 但我无法更改 @days 声明。以下代码也无法正常工作:-
@days = [a,2,3];
use feature qw<say>;
foreach(@days){print "$_\n";}
print "\n\n @days\n";
print "@$days\n";
say $_ for $days->@*;
解决方案
Attn: OP - 数组声明不正确。
如果您无法更改数组声明(不清楚是什么原因),请使用以下代码打印它们
use strict;
use warnings;
use feature 'say';
my @days = ['a',2,3];
say for @{$days[0]};
say "Number of elements: " . scalar @{$days[0]};
正确的代码应该是
use strict;
use warnings;
use feature 'say';
my @days = ('a',2,3);
say for @days;
say "Number of elements: " . scalar @days;
以下代码演示了数组是如何创建的,使用这些信息很容易弄清楚如何访问数组元素的存储值
use strict;
use warnings;
use feature 'say';
use Data::Dumper;
my @days = ['a',2,3];
say Dumper(\@days);
输出
$VAR1 = [
[
'a',
2,
3
]
];
推荐阅读
- javascript - 为什么要将函数传递给反应组件以获取数据而不是数据本身?
- python - 如何迭代 MeshGrid?
- quarkus - 是否有任何 Quark 扩展或对 aws secret manager 的支持?
- c++ - 反向迭代器未按预期工作 C++
- c# - 通过 X509Store 调用时,有效证书不再有效
- ssl - 结构化流式传输未能读取我的 MSK Kafka 并继续删除 executorsId
- java - 我在 Java 中实现“while”循环时遇到问题
- python - 硒获取我当前的位置“位置”
- typescript - Typescript:如何将导出的默认模块的类型动态推断为对象
- c# - 使用 ajax 调用时,RowGroup 不起作用