您如何在Perl中引用列表? [英] How do you reference a list in Perl?

查看:66
本文介绍了您如何在Perl中引用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到有关scalarsarrayslist的信息.我不确定list是什么意思.例如,(5, apple, $x, 3.14)list,但是实际引用该列表的变量是什么?

I read about scalars arrays and list. I am not sure what is meant by list. For example, (5, apple, $x, 3.14) is a list, but what is the variable actually referencing the list?

是否仅列出了一种初始化数组的方法,或者它是已知的数据结构?

Are lists just a way to initialize arrays or is it the known data structure?

推荐答案

Perl中存在三种不同的基本数据结构.

There are three different basic data structures in Perl.

  • 哈希:这些是键/值对.
  • 数组:这些是有序的值.
  • 标量:这是一个单一值.

哈希:

my %employee = (
    "A001" => "Albert",
    "A002" => "Bob",
    "B003" => "Gary",
);

print "$employee{A001}\n";   #Prints "Albert"
print "$employee{B003}\n";   #Prints "Gary"

数组:

my @fruit = ("Apple", "Orange", "Banana");
print "$fruit[0]\n";   #Prints "Apple"
print "$fruit[2]\n";   #Prints "Banana"

标量:

my $age = "None of your business";
print "You're $age years old\n";  #Prints "You're None of your business years old

列表仅仅是项目列表.例如,@fruit是一个数组.但是,我将数组设置为等于包含水果名称的列表.

A List is merely a list of items. For example, @fruit is an array. However, I set my array to equal a list containing the names of fruits.

从技术上讲,列表是不可更改的,而数组是可以修改的数据结构.

Technically, a list is unchangeable while an array is a data structure that can be modified.

for my $color ("red", "green", "blue") {
    print "$color is an item from my list\n";
}

在上面,我的for循环通过列表:

In the above, my for loop advances through a list:

my @list = ("red", "green", "blue");
for my $color (@list) {
   print "$color is a member of my array\n";
}

上面的代码几乎相同,但是现在我的for循环通过数组前进.

The above is pretty much the same code, but now my for loop advances through an array.

这篇关于您如何在Perl中引用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆