Dear Sirs,
I attempted to write a piece of script that takes data like below as example.
__DATA__ ABCD EFGH
My intention is to create a Hash of Array. The arrays should contain columnwise substring of length (in this case) 2 from each lines (all line is of same length). Furthermore, the keys should contain the position (index) of the substring from the full line. Such that the result would be as follows:
$VAR1 = { '0' => [ 'AB', EF ], '1' => [ 'BC', 'FG' ], '2' => [ 'CD', 'GH', ] };
The following script of mine are unable to obtain those results.
#!/usr/bin/perl -w use strict; use Data::Dumper; my $enum_size =0; my %hash; my $sub_length = 2; my $lmer; while( <DATA> ) { chomp; my @array; $enum_size = length ($_) - $sub_length +1; for (my $j =0 ;$j <$enum_size ;$j++) { $lmer = substr ($_, $j, $sub_length); push @array, $lmer; $hash{$j}=[@array]; } } print Dumper \%hash; #Then do sth with the %hash
Please advice how can I approach this problem. Thanks so much beforehand.
And Merry Christmas to you all too!
Regards,
Edward

In reply to 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.