Perl`join`生成多行字符串

Perl`join`生成多行字符串,perl,Perl,我有这个程序对两个数组进行排序 #!/usr/bin/perl -w $movies = 'movies.txt'; open (FHD, $movies) || die " could not open $movies\n"; @movies = <FHD>; $fruits = 'fruits.txt'; open (FHD, $fruits) || die " could not open $fruits\n"; @fruits = <FHD>; @array

我有这个程序对两个数组进行排序

#!/usr/bin/perl -w

$movies = 'movies.txt';
open (FHD, $movies) || die " could not open $movies\n";
@movies = <FHD>;

$fruits = 'fruits.txt';
open (FHD, $fruits) || die " could not open $fruits\n";
@fruits = <FHD>;

@array3 = (@movies , @fruits);
@array3 = sort @array3;

print @array3;
我怎样才能把它改成这样

apple, gi joe, iron man, orange, pear, star trek, the blind side
我知道它与
join
有关,但如果我将程序更改为此,它仍会在多行上打印输出

$value = join(', ', @array3); 
print "$value\n";

数组中的数据在从文件读取的每行末尾仍有换行符。用于修复此问题

您还应该在每个Perl程序的顶部
使用strict
使用warnings

最佳做法是将词法文件句柄与的三参数形式一起使用,
die
字符串应包含内置的
$
变量,用于说明
打开失败的原因

use strict;
use warnings;

my $movies = 'movies.txt';
open my $fh, '<', $movies or die "Could not open '$movies': $!\n";
my @movies = <$fh>;
chomp @movies;


my $fruits = 'fruits.txt';
open $fh, '<', $fruits or die "Could not open '$fruits': $!\n";
my @fruits = <$fh>;
chomp @fruits;

my @array3 = sort @movies, @fruits;

print join(', ', @array3), "\n";

数组中的数据在从文件读取的每行末尾仍有换行符。用于修复此问题

您还应该在每个Perl程序的顶部
使用strict
使用warnings

最佳做法是将词法文件句柄与的三参数形式一起使用,
die
字符串应包含内置的
$
变量,用于说明
打开失败的原因

use strict;
use warnings;

my $movies = 'movies.txt';
open my $fh, '<', $movies or die "Could not open '$movies': $!\n";
my @movies = <$fh>;
chomp @movies;


my $fruits = 'fruits.txt';
open $fh, '<', $fruits or die "Could not open '$fruits': $!\n";
my @fruits = <$fh>;
chomp @fruits;

my @array3 = sort @movies, @fruits;

print join(', ', @array3), "\n";

数组中的数据在从文件读取的每行末尾仍有换行符。用于修复此问题

您还应该在每个Perl程序的顶部
使用strict
使用warnings

最佳做法是将词法文件句柄与的三参数形式一起使用,
die
字符串应包含内置的
$
变量,用于说明
打开失败的原因

use strict;
use warnings;

my $movies = 'movies.txt';
open my $fh, '<', $movies or die "Could not open '$movies': $!\n";
my @movies = <$fh>;
chomp @movies;


my $fruits = 'fruits.txt';
open $fh, '<', $fruits or die "Could not open '$fruits': $!\n";
my @fruits = <$fh>;
chomp @fruits;

my @array3 = sort @movies, @fruits;

print join(', ', @array3), "\n";

数组中的数据在从文件读取的每行末尾仍有换行符。用于修复此问题

您还应该在每个Perl程序的顶部
使用strict
使用warnings

最佳做法是将词法文件句柄与的三参数形式一起使用,
die
字符串应包含内置的
$
变量,用于说明
打开失败的原因

use strict;
use warnings;

my $movies = 'movies.txt';
open my $fh, '<', $movies or die "Could not open '$movies': $!\n";
my @movies = <$fh>;
chomp @movies;


my $fruits = 'fruits.txt';
open $fh, '<', $fruits or die "Could not open '$fruits': $!\n";
my @fruits = <$fh>;
chomp @fruits;

my @array3 = sort @movies, @fruits;

print join(', ', @array3), "\n";


我只在运行新程序时收到西瓜,其余数据没有出现@用户3448790:你的文件来自哪里?我怀疑您正在Linux系统上使用Windows系统的数据运行程序?@user3448790:若要解决此问题,请将@movies的
chomp@movies
替换为
s/\s+\z//,并使用Windows cygwin运行
@fruits
Im,请问您的建议是什么意思?Windows使用
CRLF
来终止文本文件中的每一行,而Linux只使用
LF
。因此,在类似Linux的系统上,
chomp
将只删除
LF
,留下
CR
,从而使多行相互覆盖。替换
s/\s+\z/
删除字符串末尾的所有“空白”,包括
CR
LF
以及
HT
FF
和空格,因此无论有什么,它都会正确删除行结尾。我只在运行新程序时收到西瓜,其余的数据没有出现@用户3448790:你的文件来自哪里?我怀疑您正在Linux系统上使用Windows系统的数据运行程序?@user3448790:若要解决此问题,请将@movies的
chomp@movies
替换为
s/\s+\z//,并使用Windows cygwin运行
@fruits
Im,请问您的建议是什么意思?Windows使用
CRLF
来终止文本文件中的每一行,而Linux只使用
LF
。因此,在类似Linux的系统上,
chomp
将只删除
LF
,留下
CR
,从而使多行相互覆盖。替换
s/\s+\z/
删除字符串末尾的所有“空白”,包括
CR
LF
以及
HT
FF
和空格,因此无论有什么,它都会正确删除行结尾。我只在运行新程序时收到西瓜,其余的数据没有出现@用户3448790:你的文件来自哪里?我怀疑您正在Linux系统上使用Windows系统的数据运行程序?@user3448790:若要解决此问题,请将@movies的
chomp@movies
替换为
s/\s+\z//,并使用Windows cygwin运行
@fruits
Im,请问您的建议是什么意思?Windows使用
CRLF
来终止文本文件中的每一行,而Linux只使用
LF
。因此,在类似Linux的系统上,
chomp
将只删除
LF
,留下
CR
,从而使多行相互覆盖。替换
s/\s+\z/
删除字符串末尾的所有“空白”,包括
CR
LF
以及
HT
FF
和空格,因此无论有什么,它都会正确删除行结尾。我只在运行新程序时收到西瓜,其余的数据没有出现@用户3448790:你的文件来自哪里?我怀疑您正在Linux系统上使用Windows系统的数据运行程序?@user3448790:若要解决此问题,请将@movies的
chomp@movies
替换为
s/\s+\z//,并使用Windows cygwin运行
@fruits
Im,请问您的建议是什么意思?Windows使用
CRLF
来终止文本文件中的每一行,而Linux只使用
LF
。因此,在类似Linux的系统上,
chomp
将只删除
LF
,留下
CR
,从而使多行相互覆盖。替换
s/\s+\z/
删除字符串末尾的所有“空白”,其中包括
CR
LF
以及
HT
FF
和空格,因此它将删除行结尾co