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

I wanted to have in a result: %h === (R => 7, T => 12), but I have %h === (1 => 19), because hash asks for scalar context (at line 2):
my %h = (); $h{ /\w$/g # <-- HERE } += $_ while <DATA>; {local $, = "\n"; print %h} __DATA__ 5 R 3 T 9 T 2 R

Replies are listed 'Best First'.
Re: How to force list context?
by choroba (Cardinal) on Dec 08, 2014 at 11:14 UTC
    You can use a hash slice with only one key:
    @h{ /\w$/g } += $_ while <DATA>;

    But I find it quite ugly. What about split?

    $h{ (split)[1] } += $_ while <DATA>;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks!
Re: (Answered) How to force list context?
by LanX (Saint) on Dec 08, 2014 at 18:27 UTC
    Well maybe not exactly your question, but you have always list context within inside @{[...]} even within double quotes interpolation.

    Personally I find your code with implicit "ASCII to integer" conversion of $_ far too magic.

    Assigning explicit vars like ($val,$key)=split doesn't take much more code and is self documenting.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)