English 中文(简体)
我怎么能够用每条线的限量打印N阵列元素?
原标题:How can I print N array elements with delimiters per line?
  • 时间:2010-03-25 19:02:15
  •  标签:
  • perl

我在Perl有一阵列,希望与每个单元之间的空间划界员一道印刷,但每10个部分应划定新线。 如果是这样的话,这些要素没有任何空间。

我写了与反之道,但我很想知道,有更好的/可恶的/可恶的Perl方式,或许是特别的结合。

我的职能是说明:

sub PrintArrayWithNewlines
{
    my $counter = 0;
    my $newlineIndex = shift @_;

    foreach my $item (@_)
    {
        ++$counter;
        print "$item";
        if($counter == $newlineIndex)
        {
            $counter = 0;
            print "
";
        }
        else
        {
            print " ";
        }
    }
}
最佳回答

参看splice。 从事类似工作:

sub PrintArrayWithNewlines {
    my $n = 10;
    my $delim = " ";
    while (my @x = splice @_, 0, $n) {
        print join($delim, @x), "
";
    }
}
问题回答

http://search.cpan.org/~adamk/List-MoreUtils-0.33/lib/List/MoreUtils.pm#natatime”rel=“nofollow noreferer” 清单:

use warnings; use strict;

use List::MoreUtils qw( natatime );

my @x = (1 .. 35);

my $it = natatime 10, @x;

while ( my @v = $it->() ) {
    print "@v
"
}

产出:

C:Temp> x
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35

如果你不想使用任何外部单元,你可以使用阵列:

use warnings; use strict;

my @x = (1 .. 95);
my $n = 10;

for my $i ( 0 .. int @x/$n ) {
    no warnings  uninitialized ;
    print "@x[$n * $i .. $n * ($i + 1) - 1]
";
}

。 解决这一问题:

use List::Gen;

for (every 10 =>  a  ..  z ) {
    print "@$_
"
}

#   a b c d e f g h i j
#   k l m n o p q r s t
#   u v w x y z

也可以写

foreach (by 10 =>  a  ..  z ) {
    print "@$_
"
}

或利用职能表格:

mapn {print "@_
"} 10 =>  a  ..  z ;  # @_ not @$_ here

您的风格:

my $letters = by 10 =>  a  ..  z ;

while (my $line = $letters->next) {
   print "@$line
";
}

也可使用<条码><<>>>>>>,并修改<条码>。

#!/usr/bin/perl -w

use strict;

sub PrintArrayWithNewlines 
{    
    my @array = @_;
    my $newlineIndex = 10;

    foreach my $item (@array) {
        ++$globalCounter;
        print "$item";
        if ($globalCounter == $newlineIndex) {
            $globalCounter = 0;
            print "
";
        }
        else {
            print " ";
        }
    }
}

my $globalCounter = 0;
my @myArray =  a  ..  z 
map { PrintArrayWithNewlines($_) } @myArray;
print "
";

产出如下:

$ ./test.pl
a b c d e f g h i j
k l m n o p q r s t
u v x y z




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签