简体   繁体   English

Perl格式化数组输出。

[英]Perl formatting array output.

I have a small program that I am trying to format the output. 我有一个小程序,正在尝试格式化输出。 The results get loaded in to an array - I am just having trouble formating the printing out the array into a certain format. 结果被加载到数组中-我只是在将打印出来的数组格式化为某种格式时遇到麻烦。

#!/usr/bin/perl
use strict ;
use warnings ;
my @first_array ;
my @second_array  ;
my @cartesian ;

while (<>) {
    my $first_input =  $_ ;
    @first_array = split(' ', $first_input) ;
    last ;
}
while (<>) {
    my $second_input = $_ ;
    @second_array = split(' ', $second_input) ;
    last ;
}


while(my $first=shift(@first_array)) {
    push(@cartesian, $first) ;
        my $second = shift(@second_array)  ;
        push(@cartesian, $second ) ;

}

print "This is the merged array: @cartesian\n" ;

When I enter this in, I get this: 当我输入此内容时,我得到以下内容:

$ ./double_while2.pl
1 2 3
mon tue wed
This is the merged array 1 mon 2 tue 3 wed

what I want to print out is : 我要打印的是:

"1", "mon",
"2", "tue" ,
"3", "wed",

or alternately: 或者:

1  => "mon",
2  => "tue",
3  => "wed,

May I suggest a hash, since you are pairing things 我可以建议使用哈希,因为您正在配对

my %cartesian; 
@cartesian{ @first_array } = @second_array;

print "$_ => $cartesian{$_}\n" for sort keys %cartesian;

A hash slice is used above. 上面使用了哈希片 See Slices in perldata 请参见perldata中的切片

The arrays that you build had better pair up just right, or there will be errors. 您构建的阵列最好正确配对,否则会出错。

If the goal is to build a data structure that pairs up elements, that can probably be done directly, without arrays. 如果目标是建立一个将元素配对的数据结构,则可以直接完成而无需数组。 More information would help to comment on that. 更多信息将有助于对此发表评论。

Try to use hash instead. 尝试改用哈希。

for my $i(0..$#first_array){
    $hash{$first_array[$i]} = $second_array[$i];
 }

or else, you want format without using hashes, try as follows 否则,您需要不使用哈希的格式,请尝试以下操作

for  (my $i = 0; $i < $#cartesion/2; $i++) {
      my $j  = ($cartesion/2) + $i;
      print "$cartesion[$i] $cartesion[$j] \n";

   } 

From your question and your code, I suppose that you are a lovely new 'victim' to perl ~ To merge two arrays with same lengh, I suggeest using 'map' to simplify your code: 从您的问题和代码中,我认为您是perl的一个可爱的新“受害者”〜要合并两个长度相同的数组,我建议使用“ map”来简化您的代码:

my @cartesian = map {$first_array[$_], $second_array[$_]} 0..$#first_array;

and to format print style , you can define a subroutine to meet your different requirements: 并设置打印样式的格式,您可以定义一个子例程来满足您的不同要求:

sub format_my_array{
   my $array_ref = shift;
   my $sep = shift;
   print $array_ref->[$_],$sep,$array_ref->[$_+1],"\n" for grep {! ($_%2)} 0..$#$array_ref;
}

Now, you can try calling your subroutine: 现在,您可以尝试调用子例程:

format_my_array(\@cartesian, " => ");

or 要么

format_my_array(\@cartesian, " , ");

Now, you get what you want~ 现在,您得到了想要的东西〜

You may have noticed that some intermediate concepts are used in this answer, don't doute , that's exactly what I'm trying to introduce you to ~ 您可能已经注意到,此答案中使用了一些中间概念,请不要怀疑,这正是我要向您介绍的内容〜

May you the great happiness in learning perl ~ 愿您在学习Perl的过程中倍感高兴〜

The trick is to go with Perl's strengths instead of fighting against them: 诀窍是利用Perl的优势而不是与之抗争:

#!/usr/bin/perl

use strict;
use warnings;
# For say()
use 5.010;

my @first_array  = split ' ', <>;
my @second_array = split ' ', <>;

if (@first_array != @second_array) {
  die "Arrays must be the same length\n";
}

my @cartesian = map { $first_array[$_], $second_array[$_] } 0 .. $#first_array;

for (0 .. $#cartesian / 2) {
  say "$cartesian[$_*2] => $cartesian[$_*2+1]";
}

But, it gets much easier still if you use a hash instead of an array for your merged data. 但是,如果您使用散列而不是数组来合并数据,则变得更加容易。

#!/usr/bin/perl

use strict;
use warnings;
# For say()
use 5.010;

my @first_array  = split ' ', <>;
my @second_array = split ' ', <>;

if (@first_array != @second_array) {
  die "Arrays must be the same length\n";
}

my %cartesian;
@cartesian{@first_array} = @second_array;

for (sort keys %cartesian) {
  say "$_ => $cartesian{$_}";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM