Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: How to I check if a value is present in an array?

by Anonymous Monk
on Aug 03, 2013 at 13:28 UTC ( #1047705=note: print w/replies, xml ) Need Help??

in reply to How to I check if a value is present in an array?

Also, you might consider another type of data-structure, such as a hash, keyed by the value of the gene-string, whose value is the number of times that this particular string occurs.   This is remarkably easy to do in Perl:

my $h={};
$h->{ $_ } ++ foreach (@my_array);

After initializing $h to an empty hashref, we loop over all elements in @my_array (each element being briefly represented by the variable $_), and increment the value of the hash-entry under that key.   Welcome to Perl’s “auto-vivication” feature:   each hash-element is magically created as necessary, and since you are “incrementing” it, the initial value automagically starts at zero.

This representation is functionally-equivalent to an array of discrete strings, but much faster to process.

  • Comment on Re: How to I check if a value is present in an array?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047705]
[Eily]: duck typing is when someone starts typing in python you have to duck, fast
[shmem]: trading perl for python is just like throwing away a big tool box, to be given a hammer, a gripper and a screwdriver instead

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2018-04-20 13:10 GMT
Find Nodes?
    Voting Booth?