gpt4 book ai didi

perl - 如何将 __DATA__ 读入数组以便每个单词都有自己的索引?

转载 作者:行者123 更新时间:2023-12-01 01:13:56 24 4
gpt4 key购买 nike

我正在尝试打印字符串 whiteacer使用以下代码但它不起作用:

@a=<DATA>;
print "$a[9] $a[14]";

__DATA__
one two three four five six seven
black yellow white green
apple orange
hp acer samsung

我如何阅读 __DATA__成一个数组,以便 white位于索引 9 和 acer在索引 14 处?

最佳答案

如果你用 Data::Dumper 检查你的“数组” ,您将看到它只有四个元素,因此未定义索引 '9' 和 '14':

use strict;
use warnings;

use Data::Dumper;

my @a = <DATA>;

print Dumper \@a;

# prints
#[
# 'one two three four five six seven
#',
# 'black yellow white green
#',
# 'apple orange
#',
# 'hp acer samsung
#'
#]

__DATA__
one two three four five six seven
black yellow white green
apple orange
hp acer samsung

关于perl - 如何将 __DATA__ 读入数组以便每个单词都有自己的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22203717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com