PERL:仅在没有段落的数组循环(最后一次迭代循环)上处理元素时,才能进行进一步处理

发布于 2025-02-11 22:34:17 字数 635 浏览 1 评论 0原文

我如何检查是否在数组中不存在foreach循环处理的元素?

示例:

my @array = ("abc","def","ghi");
foreach my $i (@array) {
    print "I am inside array\n";
    #####'Now, I want it further to go if there are no elements after 
    #####(or it can be said if it is the last element of array. Otherwise, go to next iteration'
    print "i did this because there is no elements afterwards in array\n";
}

我可以想到做到这一点的方法,但是想知道我是否可以使用特定的关键字或函数以短暂的方式获得它。我想的一种方式:

my $index = 0;
while ($index < scalar @array) {
    ##Do my functionality here

}
if ($index == scalar @array) {
    print "Proceed\n";
}

How can I check if no elements exist further in the array to be processed by the foreach loop?

Example:

my @array = ("abc","def","ghi");
foreach my $i (@array) {
    print "I am inside array\n";
    #####'Now, I want it further to go if there are no elements after 
    #####(or it can be said if it is the last element of array. Otherwise, go to next iteration'
    print "i did this because there is no elements afterwards in array\n";
}

I could think of ways to do this, but wondering if I can get it in a short way, either using a specific keyword or function. One way I thought:

my $index = 0;
while ($index < scalar @array) {
    ##Do my functionality here

}
if ($index == scalar @array) {
    print "Proceed\n";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

待天淡蓝洁白时 2025-02-18 22:34:17

有多种方法可以实现所需的结果,有些是基于数组的$索引的使用,而其他基于使用$#$#array-1可以用于获得的方法Array Slice,带有$ array [-1]的数组的最后一个元素。

use strict;
use warnings;
use feature 'say';

my @array = ("abc","def","ghi");

say "
  Variation #1
-------------------";
my $index = 0;

for (@array) {
    say $index < $#array 
        ? "\$array[$index] = $array[$index]" 
        : "Last one: \$array[$index] = $array[$index]";
    $index++;
}

say "
  Variation #2
-------------------";
$index = 0;

for (@array) {
    unless ( $index == $#array ) {
        say "\$array[$index] = $_";
    } else {
        say "Last one: \$array[$index] = $_";
    }
    $index++;
}

say "
  Variation #3
-------------------";
$index = 0;

for( 0..$#array-1 ) {
    say "\$array[$index] = $_";
    $index++;
}

say "Last one: \$array[$index] = $array[$index]";

say "
  Variation #4
-------------------";

for( 0..$#array-1 ) {
    say  $array[$_];
}

say 'Last one: ' . $array[-1];

say "
  Variation #5
-------------------";
my $e;

while( ($e,@array) = @array ) {
    say @array ? "element: $e" : "Last element: $e";
}

There are multiple ways to achieve desired result, some based on usage of $index of array, and other based on use $#array-1 which can be utilized to obtain the array slice, the last element of an array accessible with $array[-1].

use strict;
use warnings;
use feature 'say';

my @array = ("abc","def","ghi");

say "
  Variation #1
-------------------";
my $index = 0;

for (@array) {
    say $index < $#array 
        ? "\$array[$index] = $array[$index]" 
        : "Last one: \$array[$index] = $array[$index]";
    $index++;
}

say "
  Variation #2
-------------------";
$index = 0;

for (@array) {
    unless ( $index == $#array ) {
        say "\$array[$index] = $_";
    } else {
        say "Last one: \$array[$index] = $_";
    }
    $index++;
}

say "
  Variation #3
-------------------";
$index = 0;

for( 0..$#array-1 ) {
    say "\$array[$index] = $_";
    $index++;
}

say "Last one: \$array[$index] = $array[$index]";

say "
  Variation #4
-------------------";

for( 0..$#array-1 ) {
    say  $array[$_];
}

say 'Last one: ' . $array[-1];

say "
  Variation #5
-------------------";
my $e;

while( ($e,@array) = @array ) {
    say @array ? "element: $e" : "Last element: $e";
}
小红帽 2025-02-18 22:34:17

一种检测处理时间在最后一个元素时的一种方法

my @ary = qw(abc def ghi);

foreach my $i (0..$#ary) { 
    my $elem = $ary[$i];
    # work with $elem ...

    say "Last element, $elem" if $i == $#ary;
}

语法$#array-name用于数组中最后一个元素的索引。


还请注意,每个 在数组上有用,如果有索引的用途

while (my ($i, $elem) = each @ary) { 
    # ...
    say "Last element, $elem" if $i == $#ary;
}

,请确保阅读文档要了解每个的微妙之处。

One way to detect when processing is at the last element

my @ary = qw(abc def ghi);

foreach my $i (0..$#ary) { 
    my $elem = $ary[$i];
    # work with $elem ...

    say "Last element, $elem" if $i == $#ary;
}

The syntax $#array-name is for the index of the last element in the array.


Note also that each works on arrays, useful if there are uses of indices

while (my ($i, $elem) = each @ary) { 
    # ...
    say "Last element, $elem" if $i == $#ary;
}

Then make sure to read docs to be aware of subtleties of each.

恋竹姑娘 2025-02-18 22:34:17

取决于您要处理空数组的方式:

for my $ele ( @array ) {
    say $ele;
}

say "Proceed";

for my $ele ( @array ) {
    say $ele;
}

if ( @array ) {
   say "Proceeding beyond $array[-1]";
}

Depending on how you want to handle empty arrays:

for my $ele ( @array ) {
    say $ele;
}

say "Proceed";

or

for my $ele ( @array ) {
    say $ele;
}

if ( @array ) {
   say "Proceeding beyond $array[-1]";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文