Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Inheritance in Hash

by Miguel (Friar)
on Apr 21, 2005 at 02:59 UTC ( #449846=note: print w/replies, xml ) Need Help??

in reply to Inheritance in Hash

And another way could be something like:
#!/usr/bin/perl -w use strict; my $data = { Colour => 'blue', Entries => { Flowers => { Dahlia => { Smell => 'nice' }, Rose => { Colour => 'red' } } } }; my $ref_flowers = $data->{Entries}->{Flowers}; my $colour = sub{my $fl=shift; $ref_flowers->{$flw}->{Colour}}; my $smell = sub{my $fl=shift; $ref_flowers->{$flw}->{Smell}}; print "Rose's color is ",$colour->('Rose'),".\n" if $colour->('Rose'); print "Dahlia smells ",$smell->('Dahlia'),".\n" if $smell->('Dahlia'); __OUTPUT__ Rose's color is red. Dahlia smells nice.

Replies are listed 'Best First'.
Re^2: Inheritance in Hash
by ikegami (Pope) on Apr 21, 2005 at 04:21 UTC
    print "Dahlia's color is ",$colour->('Dahlia'),".\n"
    doesn't print "blue", as requested.
      You're right.
      #!/usr/bin/perl -w use strict; my $data = { Colour => 'blue', Entries => { Flowers => { Dahlia => { Smell => 'nice' }, Rose => { Colour => 'red' } } } }; my $ref_flowers = $data->{Entries}->{Flowers}; my $colour = sub { my $flw=shift; $ref_flowers->{$flw}->{Colour} || $data->{Colour} }; print "Rose's color is ",$colour->('Rose'),"\n"; print "Dahlia's color is ", $colour->('Dahlia'),"\n"; __output__ Rose's color is Red. Dahlia's color is Blue.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://449846]
[1nickt]: Good morning Monks and Monkettes!
[1nickt]: I have encountered a small code snippet I can not decipher: if ($?) { my $status = $? >> 8; ... }
[1nickt]: Hm, found the doc
[hippo]: >> is the bitwise shift, essentially divides the result by 256.
[haukex]: $? and system - the exit code of a process is stored in the upper bits of $?, so >> 8 does a bit shift to get at it
[hippo]: (truncating)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2017-12-13 11:32 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (358 votes). Check out past polls.