简体   繁体   中英

explaination of $logline{$cod}{s1} = scalar keys %{$valid{$cod}};

What does the following perl code aim to do?

$logline{$cod}{s1} = scalar keys %{$valid{$cod}};

"valid" should be treated as a hashref, and $cod should be treated as a key. Is that right?

what does "s1" in the left hand stand for, a key again?

It stores the number of elements in the hashref referenced by $valid{$cod} into the LHS.

"valid" should be treated as a hashref,

No, "valid" is the name of the %valid hash and $valid{} accesses one of the values in the hash.

$cod is a hash key in both places. "s1" is a hash key also.

RHS

Get the value of %valid indexed by $cod .

$valid{$cod}

Treat that value as if it were a hashref.

%{$valid{$cod}}

Get a list of keys of that hashref.

keys %{$valid{$cod}}

Find out how many keys are in that list.

scalar keys %{$valid{$cod}}

(This is not how it actually works, instead keys called in scalar context returns a number representing how many elements it would have returned had it been in list context.)


LHS

The hash %logline is indexed by $cod .

$logline{$cod}

Which is itself a hashref, which is indexed by s1 .

$logline{$cod}{s1}

Bring it all together

The value of the first segment is stored at the position indicated by the second

$logline{$cod}{s1} = scalar keys %{$valid{$cod}};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM