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

前端之家收集整理的这篇文章主要介绍了perl – 如何将__DATA__读入数组,以便每个单词都有自己的索引?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用以下代码打印字符串white和acer,但它不起作用:

@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__读入数组,以便白色位于索引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相关文章