Cratylus
Cratylus

Reputation: 54084

Trying to understand context in arrays/list in Perl

I am trying to understand the context (array/list/scalar) in Perl.
I tried the following:

@array = qw (john bill george);  
print @array;  
print "\n";  
@sorted = sort (array);  
print @sorted;  
  1. Why does the print @array concats the quoted words? I need to print "@array"; to print the list? I mean @ signifies an array right? So why are the quoted needed for print?
  2. Why does the print @sorted; prints array? If it is treated as a scalar shouldn't it print 3 which is the size of the array?

Upvotes: 1

Views: 107

Answers (2)

Dave Cross
Dave Cross

Reputation: 69294

The function print takes a list of arguments and prints those arguments.

If you explicitly pass a list to print then I hope you're not surprised when it prints out the elements of that list without spaces between them.

print 'one', 'two', 'three'; # prints "onetwothree

Passing an array to print is exactly the same. The contents of the array are converted to a list and then passed to print;

my @array = qw(one two three);
print @array; # prints "onetwothree"

In both of those cases, print receives three arguments and prints those arguments with nothing separating them.

Actually, Perl uses the special variable $, to control what print outputs between its arguments. By default that's an empty string, but you can change it.

Now let's consider your other case.

my @array = qw(one two three);
print "@array"; # prints "one two three"

How many arguments does print get in this example? Well, it's just one, isn't it? It's a single double quoted string. And when Perl sees a double quoted string it expands any variables in the string. The result of that expansion is then passed to print which then prints it. So we need to find out how Perl expands arrays in double-quoted. That's defined in the perldata manual page.

Array Interpolation

Arrays and slices are interpolated into double-quoted strings by joining the elements with the delimiter specified in the $" variable ($LIST_SEPARATOR if "use English;" is specified), space by default.

So, by default, Perl interpolates arrays into double-quoted strings by inserting spaces between the elements. you can change that behaviour by changing the value of $".

These two examples might look the same, but they are actually very different.

Upvotes: 2

miorel
miorel

Reputation: 1833

print @sorted prints "array" because you forgot the @ in the previous line :P Change sort(array) to sort(@array) and it will print "billgeorgejohn".

As for why does print @array concatenate the quoted words, first let's make sure we're on the same page regarding qw:

@array = qw(john bill george);

is equivalent to

@array = ("john", "bill", "george");

so you're getting an array of three elements. Next, see the documentation for print. Passing a list of stuff to print will print them all, in order, separated by whatever value $, (the output field separator) has at the time. The default is empty string.

So you could do:

$, = " ";
print @array;

to get "john bill george".

Upvotes: 4

Related Questions