Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Pivoting 2 dimensional array refs

by John M. Dlugosz (Monsignor)
on May 14, 2011 at 15:43 UTC ( [id://904839]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Pivoting 2 dimensional array refs
in thread Transposing 2 dimensional arrays

More verbose doesn't mean easier to read. The separate branches means I have to read through and grasp that the LHS is the same, and then re-arrange it in my mind to the higher-level idea: set LHS to one of two things depending on condition.

Writing the more concise form more closely represents the higher-level meaning. That doesn't mean you have to make it overly terse: write it on separate lines:

$value= (condition) ? this : that ;
It's no different than using a for loop instead of a while, to put the different parts of the idiom together.

For sequential steps, I'll certainly write it on multiple lines with named variables for the intermediate values, rather than cramming it into one huge nested statement. But this is not like that. Spreading it out makes it harder to read.

More specialized forms, like the one he used (//), are idiomatic in Perl. open or die. $normal // $blank.

You say "optimized idioms". Well, I think we are speaking across purposes. Nesting a bunch of stuff instead of writing separate statements doesn't make it any clearer. But using a well-known idiom gives a higher-level impression of its meaning, what it's used for and the specific nature of the problem it is addressing.

If you don't know them yet, sure they are confusing because the writer doesn't need to comment in detail what those issues and solved problems are — the use of the idiom says it all. But once you learn why a particular twist of the keyboard is written that way, maybe read a whole essay about it, then you just follow that example and consider the matter settled.

As for cocnating the "defined or", you might want to start with the regular logical || and its low-precedence counterpart or. Look how it's different from C, in that the result of the expression isn't just "true" but the value from the first true argument. They are used tersely like footnotes: if that normal way didn't work out, use this instead. Or as control-flow, picking the first one that applies.

If I have a parameter $x, and the caller can leave off the parameter and mean the default value of 42, you can write, in the body of the function $x//42. Or you can set it if it's not passed, with $x //= 42;. The pulling of the data from the matrix with missing values is the same: give me xy, but if that's not available, use blank instead.

The code you learned for opening files uses this kind of control flow too:

open FILE, '<', $fname or die "Can't open $fname";

Replies are listed 'Best First'.
Re^5: Pivoting 2 dimensional array refs
by Voronich (Hermit) on May 18, 2011 at 15:32 UTC

    I think you're right. Certainly additional whitespace isn't inherently virtuous ;).

    I spent some idle cycles trying to figure out what I don't like about the $a = (b) ? c : d; notation, while being fine with the same construct in the 'open' case and as strange as it may sound I think it's that the former doesn't read in natural language very well, being "assign lvalue if condition from value otherwise from alternate." But open is "open file or die" which flows much easier when reading down the code.

    Me
      In that case, it's just a matter of getting used to the idiom, or learning to read code more fluently. Code is not English, any more than the pattern of pieces on a chessboard or an engineering blueprint. But you learn how to arrange the information in your mind and then how to extract that from the presented form.

      The mental things to take in are:

      1. assign to $a
      2. different things depending on b
      3. the specific things
      So, the presented order does match how you grasp it, even though it's not a natural order in an English sentence.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-20 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found