How to write an array formatted by columns. I'll leave OzzyOsbourne to explain the why's and wherefore's of this... he asked for it.

The problem was given an array containing values from A to G, we want to print the values in the appropriate column... or at least that's what I understood the problem to be ;-)

Update: Changed @s{split //} = 1; to @s{split //} = ();. The former works in this case but is misleading and plain sloppy. It puts 1 in the 'first' value of the hash and undef in the others. Since all we need to do is to 'autovivify' the elements of the hash so that the existance check works, this is OK. However, don't imagine that it puts '1' in all the hash values. The updated version is probably clearer.

Update: Rearranged ths code to work on an array rather than a string. Same principle though.

#!perl -w use strict; sub print_in_columns { my %s; # Push the input array into the hash to indicate which # columns are present @s{@_} = (); # Loop through the permitted values and either print # the value ( if it exists in the hash %s, or print # an empty column if it isn't print exists $s{$_} ? "$_\t" : "\t" for ('A'..'G'); print "\n"; } print_in_columns split // while <DATA>; __DATA__ ABC ADE AFG CDB

In reply to Printing in appropriate columns (For Ozzy) by Tyke

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.