Greetings, fellow monks,
I'm trying to come up with a clever way to solve the following problem and am convinced that I should be able to do it in one line (or thereabouts), but the gray matter isn't working up to full snuff. What I've got so far is:
my @x = (4, 4, 2);
my @y; my $pref = 'XX';
foreach my $chunk (1 .. @x) {
my $loc = sprintf("$pref%02d:", $chunk);
push @y, map {$loc . $_} ( 1 .. $x[$chunk-1] );
}
That gives me:
@y = qw(XX01:1 XX01:2 XX01:3 XX01:4 XX02:1 XX02:2 XX02:3
XX02:4 XX03:1 XX03:2);
which is the right output, but I feel there should be a better way to achieve the end...
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link or
or How to display code and escape characters
are good places to start.
|