The following echoes Eily's suggestion:

use strict; use warnings; my @AoA; while (<DATA>) { chomp; push @AoA, [ split '' ]; } for my $i ( 0 .. @AoA - 1 ) { for my $j ( 0 .. @{ $AoA[0] } - 1 ) { print "String $i; Char $j: '$AoA[$i]->[$j]'\n" } print "\n"; } __DATA__ String 1. Another 2 abcdefghi

Output:

String 0; Char 0: 'S' String 0; Char 1: 't' String 0; Char 2: 'r' String 0; Char 3: 'i' String 0; Char 4: 'n' String 0; Char 5: 'g' String 0; Char 6: ' ' String 0; Char 7: '1' String 0; Char 8: '.' String 1; Char 0: 'A' String 1; Char 1: 'n' String 1; Char 2: 'o' String 1; Char 3: 't' String 1; Char 4: 'h' String 1; Char 5: 'e' String 1; Char 6: 'r' String 1; Char 7: ' ' String 1; Char 8: '2' String 2; Char 0: 'a' String 2; Char 1: 'b' String 2; Char 2: 'c' String 2; Char 3: 'd' String 2; Char 4: 'e' String 2; Char 5: 'f' String 2; Char 6: 'g' String 2; Char 7: 'h' String 2; Char 8: 'i'

$AoA[$i]->[$j] explained:

$AoA[$i]->[$j] ^ ^ ^ | | | | | + - Character number in string | + - Dereferencing arrow + - String number in array

The above builds an array of arrays (AoA) by splitting each string on an empty string to create a list of the string's characters in an anonymous array whose reference is pushed onto @AoA. The nested loops iterate through the AoA, using the dereferencing arrow -> to access each character. The notation @{ $AoA[0] } - 1 was used to obtain the number of characters in the dereferenced array (effectively the string length), since you mentioned that each string had the same length.

Hope this helps!


In reply to Re: Interpolate variable name in Perl? by Kenosis
in thread Interpolate variable name in Perl? by SuzuBell

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.