行がある場合@array
:
row 1: a b
row 2: b c
row 3: c d
@array2
1 つの列にすべての要素を含むnew を取得するにはどうすればよい@array2 = a b b c c d
ですか?
ありがとう!
行がある場合@array
:
row 1: a b
row 2: b c
row 3: c d
@array2
1 つの列にすべての要素を含むnew を取得するにはどうすればよい@array2 = a b b c c d
ですか?
ありがとう!
あなたの質問はややあいまいな表現になっていますが、これはおそらくあなたが perl に慣れていないためです。perl構文で入力または期待される出力を提供していませんが、以前の回答に対する回答に基づいて、推測します:
## three rows of data, with items separated by spaces
my @input = ( 'a b', 'b c', 'c d' );
## six rows, one column of expected output
my @expected_output = ( 'a', 'b', 'b', 'c', 'c', 'd' );
これを予想される入力と出力に当てはめると、変換をコーディングする1つの方法は次のとおりです。
## create an array to store the output of the transformation
my @output;
## loop over each row of input, separating each item by a single space character
foreach my $line ( @input ) {
my @items = split m/ /, $line;
push @output, @items;
}
## print the contents of the output array
## with surrounding bracket characters
foreach my $item ( @output ) {
print "<$item>\n";
}
my @array_one = (1, 3, 5, 7);
my @array_two = (2, 4, 6, 8);
my @new_array = (@array_one, @array_two);
別のオプションは次のとおりです。
use Modern::Perl;
my @array = ( 'a b', 'b c', 'c d' );
my @array2 = map /\S+/g, @array;
say for @array2;
出力:
a
b
b
c
c
d
map
のリストで動作し、@array
その中の各要素に正規表現 (空白以外の文字と一致) を適用して、 に配置される新しいリストを生成し@array2
ます。
初期データのもう 1 つの考えられる解釈は、配列参照の配列があるというものです。これは 2D 配列のように「見える」ため、「行」について話すことになります。その場合は、これを試してください
#!/usr/bin/env perl
use warnings;
use strict;
my @array1 = (
['a', 'b'],
['b', 'c'],
['c', 'd'],
);
# "flatten" the nested data structure by one level
my @array2 = map { @$_ } @array1;
# see that the result is correct
use Data::Dumper;
print Dumper \@array2;