Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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

by Anonymous Monk
on Sep 05, 2011 at 18:33 UTC ( #924284=note: print w/replies, xml ) Need Help??

in reply to making presence/absence table from a hash of arrays


If you a hash and an array, why are you posting a word question (ie homework)?

How do I post a question effectively?, PerlMonks FAQ: Is PerlMonks a good place to get answers for homework?

  • Comment on Re: making presence/absence table from a hash of arrays

Replies are listed 'Best First'.
Re^2: making presence/absence table from a hash of arrays
by reubs85 (Acolyte) on Sep 06, 2011 at 09:27 UTC

    Thanks for the responses everyone,

    Incidentally, it is not 'homework'; I am a PhD genome biologist still getting to grips with the finer details of Perl - I am going to use the code as part of a script that will allow me to count genes which are shared across different species. The example I wrote was merely for ease of readership.

    So whilst its true that I should learn more about the grep and map functions (although asking questions is part of learning, I think), I'm not skipping my way through some homework assignment.

    Thanks again!

      It is certainly possible to get a lot done without fancy map statements. There is certainly something to be said for doing something straightforward with foreach loops. Don't worry about being compact/terse - do something that is easy for you to understand - worry about more complex constructs when you are writing a lot more Perl.

      See code below. Perl is great at translating one thing into another thing - the hash table. So I just make a hash table table to translate the column name into an array index. This also perhaps could have been just statically declared, but I wanted to make this flexible. For each row in the table, I just zero out an array and use the name2Index translator to turn on the appropriate elements and then print that row.

      #!/usr/bin/perl -w use strict; use Data::Dump qw(pp); my $header_row= 'one two three four five'; my %table = ( row_1 => [qw(one five two)], row_2 => [qw(four two)], row_3 => [qw(three one five four)], ); my %name2Index; my $col=0; foreach my $col_head (split ' ',$header_row) { $name2Index{$col_head} = $col++; } print "name2Index table = ",pp(\%name2Index),"\n\n"; foreach my $row (sort keys %table) { my @bitmap = (0) x keys %name2Index; foreach my $col_name (@{$table{$row}}) { $bitmap[$name2Index{$col_name}] = 1; } print "$row = @bitmap\n"; } __END__ name2Index table = { five => 4, four => 3, one => 0, three => 2, two = +> 1 } row_1 = 1 1 0 0 1 row_2 = 0 1 0 1 0 row_3 = 1 0 1 1 1
        my $header_row= 'one two three four five'; ... my %name2Index; ... my $col=0; foreach my $col_head (split ' ',$header_row) { $name2Index{$col_head} = $col++; }

        Or more simply as:

        my @header_row = qw/ one two three four five /; ... my %name2Index; ... @name2Index{ @header_row } = 0 .. $#header_row;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://924284]
and the monastery is silent...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-02-21 15:50 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (283 votes). Check out past polls.