Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re^3: making presence/absence table from a hash of arrays by Marshall
in thread making presence/absence table from a hash of arrays by reubs85

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (1)
    As of 2018-05-28 00:21 GMT
    Find Nodes?
      Voting Booth?