Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

strange looking code

by Jonathan (Curate)
on Oct 20, 2000 at 17:57 UTC ( [id://37659]=perlquestion: print w/replies, xml ) Need Help??

Jonathan has asked for the wisdom of the Perl Monks concerning the following question:

I've come across some Perl code that I don't understand.
Could someone please explain what is going on with here!
#!/usr/local/bin/perl -w use strict; my %A=(); my %r; $r{a} = "A"; $r{b} = "B"; $r{c} = "C"; $r{d} = "D"; $r{e} = "E"; $r{f} = "F"; $r{g} = "G"; $r{h} = "H"; $r{i} = "I"; $r{j} = "J"; @A{keys %r} = keys %r; print %A, "\n";

Which produces the following output...
aabbccddeeffgghhiijj


"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

Replies are listed 'Best First'.
Re: strange looking code
by kilinrax (Deacon) on Oct 20, 2000 at 18:08 UTC
    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; }
RE: strange looking code
by BlaisePascal (Monk) on Oct 20, 2000 at 18:11 UTC
    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?

      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
        I believe @foo{ LIST } = LIST is a hash slice, just by way of terminology.

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

RE: strange looking code
by runrig (Abbot) on Oct 20, 2000 at 19:38 UTC
    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.
RE: strange looking code
by Jonathan (Curate) on Oct 20, 2000 at 18:09 UTC
    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
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://37659]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-26 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found