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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |