There is an array
@array=('a','b','c');
Who cares if I turn like that
print @array[2];
or so
print $array[2];
The same situation with hashes.
There is an array
@array=('a','b','c');
Who cares if I turn like that
print @array[2];
or so
print $array[2];
The same situation with hashes.
Scalar value
@array[2]
better written as$array[2]
at -e line 1 (# 1) (W syntax) You've used an array slice (indicated by @) to select a single element. Generally it's better to ask for a scalar value (indicated by $). The difference is that you’ll find out what you want to do. If you are expecting only one subscript.If you’re not really interested in what to do, then you’ll See perlref.
If you answer briefly and accurately the question posed, then in this particular case there is absolutely no difference.
And if you delve into the details, you should talk about scalar values, array values and scalar context and array context. print @array [2]; - array value in the context of an array. print $ array [2]; - scalar value in the context of an array. There is no difference. The difference will be if you try to use an array value in a scalar context. Most often this will result in the size of the array.
Try to execute and analyze such code:
print '01: ', @array[2], "\n"; print '02: ', $array[2], "\n"; print '03: ', @array[1,2], "\n"; print '04: ' . @array[2] . "\n"; print '05: ' . $array[2] . "\n"; print '06: ' . @array[1,2] . "\n"; print '07: ' . join('', @array[1,2]) . "\n";
The difference between a comma and a point in this case is that with a comma we pass to the print a set of strings, which (all in order) the function should print, and at the point we collect (concatenate) one line, which we then pass to the function print to print.
my $s = @array[2] - не грамотная запись. my @s = @array[2..4] - нормуль. my $s = $array[2] - пиши только так!
In theory, if you first register 'use strict', then you should swear at the first entry (this is just in theory - maybe I’m wrong)
In general, the 1st character says there, which returns this record.
Source: https://ru.stackoverflow.com/questions/20559/
All Articles