Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Help me understand this idiomatic hash from array code

by sundialsvc4 (Abbot)
on Jul 11, 2017 at 20:01 UTC ( [id://1194862]=note: print w/replies, xml ) Need Help??


in reply to Help me understand this idiomatic hash from array code

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^2: Help me understand this idiomatic hash from array code
by 1nickt (Canon) on Jul 11, 2017 at 20:33 UTC

    "could have been stated in just this way: %fields = { id => 0, name => 1, age => 2};"

    Assign a key/value pair to a hash:

    %hash = ( key => "value" );

    Assign a hashref as a hash key with no value:

    %hash = { key => "value" };

    perl -MData::Dumper -wE 'my %fields = { id => 0, name => 1, age => 2}; + say Dumper \%fields;' Reference found where even-sized list expected at -e line 1. $VAR1 = { 'HASH(0x7f9d5a804850)' => undef };
    Just sayin'.


    The way forward always starts with a minimal test.
Re^2: Help me understand this idiomatic hash from array code
by shmem (Chancellor) on Jul 12, 2017 at 08:47 UTC
    could have been stated in just this way: %fields = { id => 0, name => 1, age => 2};
    ...
    you must check for correctness
    v-e-r-y carefully

    People are constantly downvoting your posts? That's why.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re^2: Help me understand this idiomatic hash from array code
by Anonymous Monk on Jul 11, 2017 at 20:26 UTC

    In my opinion, the first code example is unnecssarily complicated – notice that it “sent you to Camel book, to no avail.” It all comes down to this, and could have been stated in just this way: %fields = { id => 0, name => 1, age => 2};

    It all comes down to, the information the OP already provided?

    How can you be confused about what the OP knows when you're actively copy/pasting what he knows?

    Calling a hash slice "unnecssarily complicated" is very silly and very telling

    because future bug-sleuths will naturally tend to read the human comment and not verify that the code in fact matches it.)

    Thats what non-programmers naturally do because they can't read code

    Programmers would ignore the comments and read the code

Re^2: Help me understand this idiomatic hash from array code
by adhrain (Sexton) on Jul 12, 2017 at 08:09 UTC

    I was going to use a foreach loop, but the idiomatic code seemed (and now that I understand it, IS) better for maintainability, given that it hids indexing and is very concise

    Obviously I needed to understand its workings before employing it en masse; now that it is clear to me I can say that these are the kind of things that make me love perl and I have a new nifty piece of syntax in my arsenal.

    As for the $test variable, I agree that I could've chosen a better name (maybe $constraints like in the python original code). And given that it is just a one-off script made for fun, it is overkill to use a variable to hold the boolean, but I was influenced by habit (keep complex boolean conditions outside of the if) and by the over complex python code, where they actually create a function for every constraint and then put them in a tuple to map them to each permutation with all().

    Thank you for your observations.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: Help me understand this idiomatic hash from array code
by Anonymous Monk on Jul 11, 2017 at 21:06 UTC
    > correctness v-e-r-y carefully,

    Almost everything you say is incorrect. You're full of shit

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found