You can push directly onto the hash value,

use Data::Dumper; my %hash; my $sub_length = 2; while (<DATA>) { chomp; for my $j (0 .. length - $sub_length) { push @{$hash{$j}}, substr $_, $j, $sub_length; } } print Dumper \%hash; __DATA__ ABDC EFGH
which does what you want. I've eliminated intermediate steps and temporary variables, and replaced the C-style loop with a more perly one. The code will work even if the data lines are not all the same length. Note that the length function is called on $_ by default.

Your code appears to overwrite the results from previous data lines each time. Is that the result you see?

After Compline,
Zaxo


In reply to Re: Getting columnwise substring from multiple lines by Zaxo
in thread Getting columnwise substring from multiple lines by monkfan

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.