1

I am using Perl in Ubuntu. I have assigned few values to an array and when I am printing the array it is giving some HASH values.

Can anybody assist me with this?

Here is the code.

#!/usr/bin/perl
my $VAR="you are welcome";
my @arr={'1','2','3','4'};
print @arr;
print $VAR."\n";
print "$$ \n";

Here is the output

HASH(0x140cd80)you are welcome 
12548
2
  • 8
    {} indicates a hashref. Use my @arr=('1','2','3','4'); and everything should work as expected. Commented Jul 28, 2012 at 3:34
  • Amazing. Thank you very much for your precious help Commented Jul 28, 2012 at 3:38

2 Answers 2

9

{ ... } generates an anonymous hash, and you have assigned the hash { 1 => '2', 3 => '4' } to the first and only element of @arr.

To set @arr to have four elements containg one through four, you must write

my @arr = ( 1, 2, 3, 4 );

or

my @arr = 1 .. 4;

and then print @arr will output 1234.

If you want to put spaces between the array elements you can just put the array inside double quotes. print "@arr" will output 1 2 3 4

Sign up to request clarification or add additional context in comments.

1 Comment

and some features about $"=qq(\n) <- varialbe 'list-separator'
1

Here are some other ways you can use formatting when printing an array in Perl:

print join(", ", @arr);

or

$" = ", ";
print "@arr\n";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.