Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: making presence/absence table from a hash of arrays

by Marshall (Prior)
on Sep 06, 2011 at 22:47 UTC ( #924481=note: print w/ replies, xml ) Need Help??


in reply to Re^4: making presence/absence table from a hash of arrays
in thread making presence/absence table from a hash of arrays

"Or more simply as:" I would say that is debatable.

I think that sometimes we get a bit carried away with the "zoom" of Perl and don't emphasize the basics for beginners, i.e. we would do well to consider the audience when suggesting code.

The OP is a biologist, not a SW person. The purpose of my post was to show code that only used the most basic parts of beginning Perl - something simple - both from the program logic and the syntax. Also, this code may actually run faster than some some more terse versions!

For the OP, what jwkrahn is demonstrating here is called a "hash slice". This essentially combines multiple hash assignment statements, like: $hash{a}=0; $hash{b}=1; together as one statement, could be: @hash{'a','b'}=(0,1); This is great and cool stuff, but a plain old foreach() loop is just fine. Shorter code does not necessarily run faster - in fact, sometimes it runs slower, but it is sometimes easier to write for those "in the know". Perl is loaded with idioms. Extensive use of them is not necessary to write good solid, clear, high performing code.

The syntax for a hash slice looks similar to that of an array as a hash value. @{$hash{value}}, but it is not. The HoA (Hash of Array), @{$hash{value}} is the "take home", "use it often", "get used to seeing it" message here. A hash slice is less often encountered.

Anyway, my point here is that a hash slice is probably not "easier" for a beginner to understand because of the syntax.


Comment on Re^5: making presence/absence table from a hash of arrays
Re^6: making presence/absence table from a hash of arrays
by jwkrahn (Monsignor) on Sep 06, 2011 at 22:57 UTC
    The OP is a biologist, not a SW person.

    The OP doesn't say that so I assume that you know them personally?

    - something simple -

    The OP says they have "an array with 5 elements" but you chose to store that in a string and then split it.    How is that simpler?

      The OP doesn't say that so I assume that you know them personally?
      In the level of thread that I directly replied to, the Original Poster says: "I am a PhD genome biologist still getting to grips with the finer details of Perl".

      The OP says they have "an array with 5 elements" but you chose to store that in a string and then split it.
      Well, I certainly did focus on the OP's description of the output:
      one two three four five row_1 = 1 1 0 0 1 row_2 = 0 1 0 1 0 row_3 = 1 0 1 1 1 etc...
      It looked to me like there would be line with the column names - I certainly was thinking about a normal CSV type header. If not so what? We are focusing on the wrong detail.

      I think we are getting bogged down into small details... You posted an excellent solution as did I. Let's leave it at that.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-12-21 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (104 votes), past polls