How to print Perl array

An array is one of the basic data structures in the Perl programming language. In the array elements can be of different types.

Sometimes you need to bring an array of Perl on the screen. There are several ways to do it.

Use join()

Often the best way to derive value from Perl the array on screen is to use the function join(). Using the function join() to create a string from array values, and then to bring this string to the screen. Here's an example:

▶ Run
#!/usr/bin/perl

my @arr = (1, 20, 'asdf', 100);

print join(', ', @arr);

We have identified an array @arr in which is placed a few numbers and a string, and then using join(', ', @arr) we have created a string and using print brought it to the screen. The result of this code: 1, 20, asdf, 100. This method is good because what it clear.

Use Data::Dumper

Sometimes it is necessary to look at the structure of data stored in a variable. Fits library Data::Dumper. This library comes with Perl, separately it do not put. Using Data::Dumper you can see what is in the variable. Here is an example code that displays information about the array:

▶ Run
#!/usr/bin/perl

use Data::Dumper;

my @arr = (1, 20, 'asdf', 100);

print Dumper \@arr;

The output of the program:

$VAR1 = [
          1,
          20,
          'asdf',
          100
        ];

Use print

But apart from using join() or Data::Dumper array on the screen withdraw using print:

▶ Run
#!/usr/bin/perl

my @arr = (1, 20, 'asdf', 100);

print @arr;

The result of the program:

120asdf100

As you can see, print @arr printed all the array elements together. The elements of the array in the output is not separated. Sometimes this is what you need and sometimes not.

But instead of this method print @arr is often better to write clearly print join('', @arr);. Recording with join() more easy to read man.

Variable $,

There is a special variable $,, which controls the printcombines the elements of the array in the output. For example, you can Supplement the previous example and place in this variable the symbol ,.

▶ Run
#!/usr/bin/perl

$, = ',';

my @arr = (1, 20, 'asdf', 100);

print @arr;

The program displays a text in which the elements of the array separated by commas: 1,20,asdf,100.

Variable $, may contain not only one character but several. If this variable to assign a string $, = ', ';, then the output will be a little more descriptive: 1, 20, asdf, 100.

If the variable $, to assign symbols to \n, then each element of the array will be displayed on a separate line:

▶ Run
#!/usr/bin/perl

$, = "\n";

my @arr = (1, 20, 'asdf', 100);

print @arr;

But most often it is better not to use a variable $,, and use the function join() in order to explicitly collect values from an array to a string. The problem with variable $, is that a value can be assigned in one place in the program and print that is affected by this variable is in a totally different place of the program. When using join() set to divide is immediately obvious. Another problem variable the fact that it affects all print which is located after the assignment.

ARRAY(0x561c66b32870)

Sometimes when you enter a Perl array to the screen you can see the line ARRAY(0x561c66b32870). This means that the screen will display instead of an array reference to the array. Here is an example of code that displays a string like this:

▶ Run
#!/usr/bin/perl

my @arr = (1, 20, 'asdf', 100);
my $arr_ref = \@arr;

print $arr_ref;

In order to display the array on the screen or use Data::Dumper, or to dereference the link and instead print $arr_ref; write print @{$arr_ref};. Better yet, write print join('', @{$arr_ref});

Related topics

Other articles