$b
未定義です。
@b
と$b
は異なる変数です。1 つはリストで、もう 1 つはスカラーです。
内容ではなく、配列の長さを出力しています。
推奨事項:
- 「警告を使用する」を使用します。
- 「use strict;」を使用してください。
- 「プッシュ@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";