Hello Monks,

I am generating disk names and need to generate a list from a to X, where X is anything from z to zz.

At the moment, I'm generating a list then taking an array slice, e.g.:

#!/usr/bin/env perl use 5.010; use strict; use warnings; my @list = "a" .. "zz"; my @disk_list = qw(foo bar baz); my @new_list = @list[ 0 .. $#disk_list ]; map { say } @new_list;

The problem is, what if I need to go to "aaa", e.g. @disk_list is 703 long, this won't allow for that, and I think will generate an error, since @list[ 0 .. 703 ] would be out of bounds.

How can I generate a list from a to an arbitrary upper limit?

Thanks!

Acutually, the full sub is:

sub build_disk_list { my $self = shift; my $disk_names = shift; my @letters = 'a' .. 'zz'; my @disk_letters = @letters[ 0 .. $#{$disk_names} ]; my $disk_it = each_array @{$disk_names}, @disk_letters; my @disk_list; while ( my ( $disk_name, $disk_letter ) = $disk_it->() ){ push @disk_list, [$disk_name, "vd" . $disk_letter ]; } return \@disk_list; }

Edit: ok, a slice with an out of bound index would just return an empty value, which is even worse than generating an error

Solved Edit: Thanks very much everyone. I think I can make one of these solutions work.


In reply to [Solved] Generate list from a to arbitrary letter by three18ti

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.