【问题标题】:Difference between "@" and "$" in PerlPerl 中“@”和“$”的区别
【发布时间】:2017-07-01 15:13:42
【问题描述】:

@variable$variable 在 Perl 中有什么区别?

我已经阅读了带有符号$ 和符号@ 在变量名前的代码。

例如:

$info = "Caine:Michael:Actor:14, Leafy Drive";
@personal = split(/:/, $info);

包含$@ 的变量有什么区别?

【问题讨论】:

  • $- 标量,@ - 数组。 $info 是一个标量字符串,包含:、@personal - 分配了拆分函数返回值的数组。 split 接受两个参数(分隔符,valueToSplit)
  • perlintro中有非常好的简洁的描述!

标签: perl


【解决方案1】:

这实际上不是关于变量,而是更多关于如何使用变量的上下文。如果你在变量名前面加上$,那么它在标量上下文中使用,如果你有一个@,这意味着你在列表上下文中使用变量。

  • my @arr; 将变量 arr 定义为数组
  • 当您想要访问一个单独的元素(即标量上下文)时,您必须使用$arr[0]

您可以在此处找到有关 Perl 上下文的更多信息:http://www.perlmonks.org/?node_id=738558

【讨论】:

  • 好答案。您还可以将$ 视为This此数字为42 $number = 42),将@ 视为These这些值为(1 . . 42) @values = (1 .. 42)最后一个值 $values[-1])
  • @arrarray,而不是 list。查看行为:print $s = @all = qw(my list here);print $s = qw(my list here);
  • 我想听听单片的列表和标量上下文,拜托。这里@one[$one]@ 是指列表上下文吗?并非在所有情况下都是如此
【解决方案2】:

当您没有感受这种语言的上下文时,您对 Perl 的所有知识都会化为乌有。

与许多人一样,您在演讲中使用单个值(标量)和一组中的许多东西。

那么,它们之间的区别:

我有一只猫。 $myCatName = 'Snowball';

它跳到床上@allFriends = qw(Fred John David);

你可以数一数$count = @allFriends;

但根本无法计算它们,因为名称列表不可数:$nameNotCount = (Fred John David);

所以,毕竟:

print $myCatName = 'Snowball';           # scalar
print @allFriends = qw(Fred John David); # array! (countable)
print $count = @allFriends;              # count of elements (cause array)
print $nameNotCount = qw(Fred John David); # last element of list (uncountable)

所以,listarray 是不一样的。

有趣的功能是你的大脑会和你开玩笑的片段:

这段代码很神奇:

my @allFriends = qw(Fred John David);
$anotherFriendComeToParty =qq(Chris);
$allFriends[@allFriends] = $anotherFriendComeToParty; # normal, add to the end of my friends
say  @allFriends;
@allFriends[@allFriends] = $anotherFriendComeToParty; # WHAT?! WAIT?! WHAT HAPPEN? 
say  @allFriends;

所以,毕竟:

Perl 有一个关于上下文的有趣特性。你的 $@ 是标记,它们帮助 Perl 了解你想要,而不是你真正的意思

$ 类似于s,所以是标量
@ 类似于a,所以是数组

【讨论】:

    【解决方案3】:

    以 $ 开头的变量是标量,单个值。

       $name = "david";
    

    以@开头的变量是数组:

       @names = ("dracula", "frankenstein", "dave");
    

    如果引用数组中的单个值,则使用 $

       print "$names[1]"; // will print frankenstein
    

    【讨论】:

      【解决方案4】:

      来自perldoc perlfaq7

      这些 $@%&* 标点符号是什么,我怎么知道何时使用它们?

      它们是类型说明符,详见 perldata:

      $ for scalar values (number, string or reference)
      @ for arrays
      % for hashes (associative arrays)
      & for subroutines (aka functions, procedures, methods)
      * for all types of that symbol name. In version 4 you used them like
        pointers, but in modern perls you can just use references.
      

      【讨论】:

        【解决方案5】:

        $ 用于标量变量(在您的情况下为字符串变量。) @ 用于数组。

        split 函数将根据提到的分隔符(:)拆分传递给它的变量并将字符串放入数组中。

        【讨论】:

        • 学究式地,split 函数将标量转换为 列表。然后分配将该列表存储在一个数组中。
        • $n 是一个标量(这里是一个数组的索引),$array[$n] 是另一个标量变量。
        【解决方案6】:
        Variable name starts with $ symbol called scalar variable.
        Variable name starts with @ symbol called array.
        
        $var -> can hold single value.
        @var -> can hold bunch of values ie., it contains list of scalar values.
        

        【讨论】:

          猜你喜欢
          • 2011-03-01
          • 2019-10-09
          • 1970-01-01
          • 1970-01-01
          • 2011-07-12
          • 2020-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多