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

Hi guys, I'm trying to assign the value of a key in a hash to a variable but want to use a hash reference. Without dereferencing the hash before on a separate line, how could I assign the value of the key to the variable while dereferencing at the same time? Here is some code:
#!/usr/bin/perl use warnings; use strict; # defines country => language hash my %langs = ( England => 'English', France => 'French', Spain => 'Spanish', China => 'Chinese', Germany => 'German'); my $countryid = 'England'; my $langsref = \%langs; # get language of England my $englishlang = %{ $langsref }{$countryid}; # <---- this doesn't wo +rk. I wanna do the assignment all in this one line but can't figure o +ut assigning while dereferencing. print $englishlang;
Any help would be great. Thanks so much!
  • Comment on assigning value of a key in a hash to a variable while dereferencing
  • Download Code

Replies are listed 'Best First'.
Re: assigning value of a key in a hash to a variable while dereferencing
by jwkrahn (Abbot) on Sep 17, 2018 at 05:20 UTC
    my $englishlang = %{ $langsref }{$countryid};
    my $englishlang = $langsref->{$countryid};
Re: assigning value of a key in a hash to a variable while dereferencing
by Athanasius (Archbishop) on Sep 17, 2018 at 06:13 UTC

    Hello pearllearner315,

    my $englishlang = %{ $langsref }{$countryid}; # <---- this doesn't wo +rk.

    Actually, it does work in Perl versions 5.20.0 and above; but only, in a sense, by accident.

    Perl 5.20 introduced a new slice syntax for arrays and hashes. So %{ $langsref }{$countryid} (formerly a syntax error) is parsed as a hash slice (of one hash element), returning the list ('England', 'English'). When this is assigned to the scalar variable $englishlang, it follows this rule:

    Binary "," is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value. This is just like C's comma operator. (perlop#Comma-Operator)

    So $englishlang contains the value 'English' after the assignment.

    jwkrahn has shown the correct dereferencing syntax.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: assigning value of a key in a hash to a variable while dereferencing
by AnomalousMonk (Archbishop) on Sep 17, 2018 at 16:01 UTC

    See perlref in general, and Using References section 3 in particular for discussion of use of the  -> (arrow) operator in dereferencing (and also see perlop). See also perldsc for (much!) more complex structures.


    Give a man a fish:  <%-{-{-{-<

Re: assigning value of a key in a hash to a variable while dereferencing
by Anonymous Monk on Sep 17, 2018 at 22:28 UTC
    Remember that leading sigils change when you access a single value.

    Dereference the hashref $foo: %{$foo} or %$foo

    Dereference the hashref $foo and return a single value: ${$foo}{$key} or $$foo{$key} or $foo->{$key}

Re: assigning value of a key in a hash to a variable while dereferencing
by BillKSmith (Monsignor) on Sep 18, 2018 at 15:21 UTC
    You are attempting to use rule 2 from the section "Using References" in Using_References". Your only mistake was using a '%' rather than '$' as the sigel. The advantage of this rule is that it always works. There are no exceptions or special cases. All the other rules produce code which is usually easier to write and certainly easier to read, but only when they apply. Other responders have already shown you how to take advantage of this in your case.
    Bill