开发者

How to pop the last element from an array of hashes?

开发者 https://www.devze.com 2023-02-05 01:55 出处:网络
The following is a reference to an array of hashes. Given such an array reference, I would like to remove the last element always. Will pop work, as hashes don\'t maintain any order?

The following is a reference to an array of hashes. Given such an array reference, I would like to remove the last element always. Will pop work, as hashes don't maintain any order?

$arrayref = [
          {
            'F1' => V1,
            'F2' => V2,
          },
          {
            'F1' => V3,开发者_开发问答
            'F2' => V4,
          },
        .
        .
        .
        ];


pop @$arrayref

or

splice(@$arrayref, -1)

or

--$#$arrayref;

For some easy to remember rules to help you use references, see http://perlmonks.org/?node=References+quick+reference.


It works. Try:

pop @{$arrayref};

Hashes don't maintain any order. But you are working with an array of objects (hashes). Arrays maintain order.

0

精彩评论

暂无评论...
验证码 换一张
取 消