Best way to iterate through a Perl array

后端 未结 6 1229
死守一世寂寞
死守一世寂寞 2020-12-23 03:03

Which is the best implementation(in terms of speed and memory usage) for iterating through a Perl array? Is there any better way? (@Array need not be retained).

6条回答
  •  感情败类
    2020-12-23 03:33

    In single line to print the element or array.

    print $_ for (@array);

    NOTE: remember that $_ is internally referring to the element of @array in loop. Any changes made in $_ will reflect in @array; ex.

    my @array = qw( 1 2 3 );
    for (@array) {
            $_ = $_ *2 ;
    }
    print "@array";
    

    output: 2 4 6

提交回复
热议问题