I assume the line that's confusing you is the following:
@A{keys %r} = keys %r;
Essentially, it's doing the same thing as the following code, which should be a lot easier to understand:
foreach $key (keys %r) {
$A{ $key } = $key;
}
| [reply] [d/l] [select] |
It looks perfectly understandable to me...
%r ends up having the keys (a,b,c,d,e,f,g,h,i,j) and their respective values (A,B,C,D,E,F,G,H,I,J)
The line @A{keys %r} = keys %r; causes %A to have the keys (a,b,c,d,e,f,g,h,i,j) and the respective values (a,b,c,d,e,f,g,h,i,j).
In the final line, print %A, "\n";, %A is being used in a list context, and thus looks like a list of key-value pairs, like so: (a,a,b,b,c,c,d,d,e,e,f,f,g,g,h,h,i,i,j,j), which gets printed out with no delimiters between each element as aabbccddeeffgghhiijj, which is what you saw.
That help?
| [reply] [d/l] [select] |
Thanks for that BlaisePascal and kilinrax, I can see what its doing just don't recognise the construct. - I first thought -w and use strict would moan and was surprised when it didn't
"We are all prompted by the same motives, all deceived by the same fallacies, all animated by hope, obstructed by danger, entangled by desire, and seduced by pleasure."
- Samuel Johnson
| [reply] |
| [reply] [d/l] |
So you say the confusing part is: @A{keys %r} = keys %r;
as someone pointed out, its a hash slice, but when someone unfamiliar
with it sees one, they usually wonder 'why is an @ in front of a
hash array?'
Well, sort of the same reason you see '$' in front
of an array or a hash when you're referring to one element of it,
because in '$hash{name}' you're referring to a scalar element of the hash,
and in '@hash{@array}' you're referring to an array of values.
This one happens to be '@A{keys %r}', which
still the same sort of thing since 'keys' returns an
array.
So you're assigning an array of values to an
array of elements, although the LHS happens to be an array of
hash elements.
| [reply] [d/l] |
No merlyn its not. - wish i was still so young! Its part of some code I now have to support but don't quite understand, I don't recognise the line @A{keys %r} = keys %r; as normal Perl.
"We are all prompted by the same motives, all deceived by the same fallacies, all animated by hope, obstructed by danger, entangled by desire, and seduced by pleasure."
- Samuel Johnson | [reply] [d/l] |