Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Fixing: Experimental keys on scalar is now forbidden

by TheVend (Novice)
on Jun 06, 2018 at 03:19 UTC ( [id://1215976]=perlquestion: print w/replies, xml ) Need Help??

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

Moving to perl 5.24.0 I get the following:
Experimental keys on scalar is now forbidden...
My line of code is:
@tempCustArray = keys { map { $_ => 1 } @webCustomers };
I call on the Monks for their wisdom.
  • Comment on Fixing: Experimental keys on scalar is now forbidden

Replies are listed 'Best First'.
Re: Fixing: Experimental keys on scalar is now forbidden
by ikegami (Patriarch) on Jun 06, 2018 at 07:27 UTC

    Better solution:

    my %seen; my @uniqWebCustomers = grep !$seen{$_}++, @webCustomers;

    Better yet:

    use List::Util qw( uniq ); my @uniqWebCustomers = uniq @webCustomers;
      I agree with the "Better yet," but in situations where I do something like option 1, I like to wrap it in a do block to maintain tight scoping:
      my @uniqWebCustomers = do{ my %seen; grep !$seen{$_}++, @webCustomers; };

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        If you're going to add a block, make it a sub.

        sub uniq { my %seen; grep !$seen{$_}++, @_ } my @uniqWebCustomers = uniq @webCustomers;
Re: Fixing: Experimental keys on scalar is now forbidden
by NetWallah (Canon) on Jun 06, 2018 at 04:11 UTC
    Try hash instead of hashref. "+" added to avoid mis-interpretation of "map":
    @tempCustArray = keys %{+ map { $_ => 1 } @webCustomers };
    UPDATE 1: That passes syntax, but does not seem to give the desired result .. still testing ....

    Update 2: This works:

    my @t = keys %{+{ +map { $_ => 1 } qw|uno dos tres| }}
    Now I have to figure out WHY it works..... and pre-Update1 does not..

    Update 3: The "+" signs are unnecessary .. this works:

    @tempCustArray = keys %{{ map { $_ => 1 } @webCustomers }}
    So - it seems to be a question of why the extra braces are required.

                    Memory fault   --   brain fried

      it seems to be a question of why the extra braces are required.

      The outer %{ ... } is a hash dereference.

      The inner { ... } creates a hash and returns a reference to it.

      In your first snippet, you have a hash dereference, but no hash.

        Thanks for the clarification, ikegami (++).

        The postfix syntax below also works, and is easier to read.

        my @t = keys {map { $_ => 1 } qw|uno dos tres| }->%*;

                        Memory fault   --   brain fried

      WOW, that worked like a champ! Thank you so much!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-24 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found