I have a string that contains 60 characters that I would like to 1) extract one at a time, 2) perform a function on (subtract 64 from the characters' ord) the extracted character and 3) place the result in a list of lists, where each of 20 elements would have 3 elements. The characters in the string are in the order they need to be in the list of lists, thusly (without the function performed on them):

@result = ( ['B', 'E', 'H'], ['A', 'C', 'J'],...); etc. etc.

I have poured through my Perl books (Advanced Perl Programming, Intermediate Perl Programming, The Perl Cookbook, etc.) and the solution hasn't presented itself to me. It's probably there, but I am not seeing it...

I do have my own solution, but in my quest for learning, I would like a more Perlish way of performing the task. Here is what I have :
#! /usr/local/bin/perl use strict; use warnings; # Setup the situation my $string = "BEHACJBDLCENADFEGOFHQAGIHJRBIKJLSCKMLNTDMOFNPOQTGPRIQSKR +TMPS"; my ($i, $j, @array, @result); # Perform the task @array = split //, $string; for $i (1..20) { for $j (1..3) { $result[$i][$j] = ord(shift(@array)) - 64; } } # Print the results for $i (1..20) { for $j (1..3) { print sprintf("%02d", $result[$i][$j]) . " "; } print "\n"; }
Because of legacy issues, I have loaded the @result array at the first element rather than the zeroth. This isn't a huge deal, but it does eliminate the need to adjust the other parts of the program.

I have attempted all manner of split, map, grep, substituions, etc. The substitution worked fine for me, but getting the results into the list of lists proved to be too much for me.

Since I actually have it doing what I want, and performance isn't really an issue, should I even be worried about devising a "one-liner" that would perform the task in one swell foop? I guess I think I should worry about...

Thanks! And another tip o' the hat to anyone who recognizes the format and the numbers in @result...

tjg...whiskey one sierra delta mike

In reply to Split a string into a list of lists by W1SDM

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.