How do dollar and number sign together work in perl?

前端 未结 3 613
天涯浪人
天涯浪人 2021-02-05 07:06

Today I have encountered a problem that required me to determine the maximum index of an array in perl. I used to do it this way:

my @array = (1, 2, 3);
print $         


        
相关标签:
3条回答
  • 2021-02-05 07:43

    This is documented in perldoc perldata, section "Scalar Values". In short, $#array is the last index of @array. As for how it works — it's sort of like an operator, but only as much as $ and @ are operators. Think of it as special syntax. The last index of an array just happens to "have a name". It's a variable that you can read from and assign to.

    0 讨论(0)
  • 2021-02-05 07:43

    That gives you the last index. It's documented in perldata - http://perldoc.perl.org/perldata.html

    0 讨论(0)
  • 2021-02-05 07:59

    The use is mentioned in first example in perldata. It denotes index of last item in the array.

    Btw, you can also use

    $array[-1]
    

    to get last item.

    0 讨论(0)
提交回复
热议问题