开发者

What mistake am I making in this code (Perl)?

开发者 https://www.devze.com 2023-03-22 08:45 出处:网络
use strict; use warnings; my %hash = (\"no1\"=>1, \"no2\"=>2, ); my @array = %hash; print @array; #Output: no11no22
use strict;
use warnings;

my %hash = ("no1"=>1,
         "no2"=>2,
        );

my @array = %hash;

print @array; #Output: no11no22
print "\n";
my $string = print @array; 
print $string; #Output: no11no221

Why $string is not same as @ar开发者_如何学JAVAray? Why am I getting 1 at the end? What mistake am I making?


The main problem is that print doesn't return a string, but rather prints out a string to a filehandle (see perldoc -f print). Instead, you can let my $string=join('',@array);


When you assign the value of print you get the value of the variable being printed and the return code, 1 for sucess. See perldoc print

0

精彩评论

暂无评论...
验证码 换一张
取 消