Accessing array elements of a reference array

I am new to Perl. I wrote a snippet to access the elements of the array and printed it on the console:

use strict;
use warnings;

my @array1 = ('20020701  00000', 'Sending Mail in Perl', 'Philip Yuson');
my @array2 = ('20020601', 'Manipulating Dates in Perl', 'Philip Yuson');
my @array3 = ('20020501', 'GUI Application for CVS', 'Philip Yuson');
my @main = (\@array1, \@array2, \@array3);

my $a = $main[0];
print @$a;
print @$a . "pdf";

First print:

20020701 00000Sending Mail in PerlPhilip Yuson

But why does the second seal deduce this?

3pdf 

I need to get an output like

20020701 00000Sending Mail in PerlPhilip Yusonpdf

I do not know why he gives 3pdf, I pressed to get out of this. Any help is appreciated.

+3
source share
5 answers

3 - the number of elements in the array. .calls the array in a scalar context, and then you get the number of elements instead of the contents of the array. you can use

print "@$a pdf";

or

print @$a , "pdf";

depending on what type of output you want.

+14

Perl, - "", Perl. :

my @fruits = qw/apples pears bananas/;
my $items = @fruits;

(⇒ ), . , , .

: , . ., . , (3), pdf.

? - Google " Perl", Perl.

+12

. Perl , ; - , .

, : , . , .

: . ; .

, . , Perl -, , . , , "" , .

@$a , . - , .. perldoc perlop, :

. .

, , @$a . @$a 3 , 3, , . .

, .

+6

print @$a . "pdf" , , 3.

, - :

print @$a, "pdf";

.

+4

, :

print "@$a.pdf", "\n";

:

my @array1 = ('20020701  00000', 'Sending Mail in Perl', 'Philip Yuson');
my @array2 = ('20020601', 'Manipulating Dates in Perl', 'Philip Yuson');
my @array3 = ('20020501', 'GUI Application for CVS', 'Philip Yuson');
my @main = (\@array1, \@array2, \@array3);

for my $x ( @main ) {
    print "@$x.pdf", "\n";
}

:

20020701  00000 Sending Mail in Perl Philip Yuson.pdf
20020601 Manipulating Dates in Perl Philip Yuson.pdf
20020501 GUI Application for CVS Philip Yuson.pdf
+3
source

Source: https://habr.com/ru/post/1722088/


All Articles