When people say "placeholders" I think "printf"... So, a variant. That reads the file with small matrix line by line:
use strict; use warnings; my @template = qw( rs12272511 rs7107801 rs11027752 rs12421837 ); my $small_matrix = <<'END'; rs12272511 rs11027752 rs12421837 rs12272511 1.0 .844 .276 rs11027752 .267 1.0 -.980 rs12421837 -.876 .374 1.0 END open my $fh, '<', \$small_matrix; my %ids = map { $_ => 1 } split ' ', <$fh>; my $fmt_id_present = join ' ', map $ids{$_} ? '%s' : '0', @template; my $fmt_id_missing = join ' ', ('0') x @template; $_ = "%s $_\n" for $fmt_id_present, $fmt_id_missing; print "\t@template\n"; for (@template) { if ( $ids{$_} ) { printf $fmt_id_present, split ' ', <$fh>; } else { printf $fmt_id_missing, $_; } }
if you find the line open my $fh, '<', \$small_matrix; strange: yes, you can open strings as files in Perl.

In reply to Re: combining multiple matrices with placeholders if row and column values in one matrix do not exist in another by Anonymous Monk
in thread combining multiple matrices with placeholders if row and column values in one matrix do not exist in another by mulder4786

Title:
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, details, 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, summary, 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.