What is $ # vvv--; do hash in perl?

This is a whole script that for some mysterious reason gives me "642"

#!usr/bin/perl 
%vvv = (1,2,3,4,5,6);
$#vvv--;
%vvv = reverse %vvv;
print keys %vvv, "\n";

And what does the “keys” do in the last statement? Thank you for your time. I'm just in a hurry that I do not have time to conduct my research. So again, I appreciate you for your contribution.

+3
source share
2 answers

$ # vvv-- looks like a comment. What happens is a hash, which is an even array of elements, just the opposite. So it happens:

%vvv = (
    1 => 2,
    3 => 4,
    5 => 6
);

at

%vvv = (
    6 => 5,
    4 => 3,
    2 => 1
);

So, when the keys are typed, it captures 642 or the new current hash keys.

+6
source

You should always run scripts with

use strict;
use warnings;

If you had, you would have noticed an error:

Global symbol "@vvv" requires explicit package name at ...

, $#vvv @vvv, . perl, @vvv %vvv - . , @vvv %vvv, .

, , , , :

my @array = 1 .. 6; # 1,2,3,4,5,6 but written as a range
$#array--;          # lower maximum index by 1
print "@array";     # prints "1 2 3 4 5"

, .

, reverse - . , . "foobar" → "raboof", , 1,2,3,4,5,6 6,5,4,3,2,1.

+20

All Articles