$b
未定义。
@b
并且$b
是不同的变量。一个是列表,另一个是标量。
您正在打印数组的长度,而不是内容。
建议:
- 使用“使用警告;”
- 使用“使用严格;”
- 使用“推@a,@b;”
你的脚本:
@a = (1,2,3); # @a contains three elements
@b= ("homer", "marge", "lisa", "maria"); # @b contains 4 elements
@c= qw(one two three); # @c contains 3 elements
print push @a, $b; # $b is undefined, @a now contains four elements
#(forth one is 'undef'), you print out "4"
print "\n";
@count_number= push @a, $b; # @a now contains 5 elements, last two are undef,
# @count_number contains one elements: the number 5
print @count_number; # you print the contents of @count_number which is 5
print "\n";
print @a; # you print @a which looks like what you started with
# but actually contains 2 undefs at the end
试试这个:
#!/usr/bin/perl
use warnings;
use strict;
my $b = 4;
my @a = (1,2,3);
my @b= ("homer", "marge", "lisa", "maria");
my @c= qw(one two three);
print "a contains " . @a . " elements: @a\n";
push @a, $b;
print "now a contains " . @a . " elements: @a\n";
my $count_number = push @a, $b;
print "finally, we have $count_number elements \n";
print "a contains @a\n";