Chankey Pathak
Chankey Pathak

Reputation: 21666

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

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 @array? Why am I getting 1 at the end? What mistake am I making?

Upvotes: 3

Views: 84

Answers (2)

marto
marto

Reputation: 4493

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

Upvotes: 5

user554546
user554546

Reputation:

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);

Upvotes: 5

Related Questions