simpzoid has asked for the wisdom of the Perl Monks concerning the following question:
Dear All,
I am looking to create an array like this:
my @arry = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9");
but am struggling to find a shorthand way of doing this. Surely there must be a way of doing this with character classes.
Scott
Re: Declaring an Array Using Character Classes
by kcott (Archbishop) on Nov 27, 2012 at 08:49 UTC
|
$ perl -Mstrict -Mwarnings -e '
my @arry = ("a" .. "z", 0 .. 9);
print "@arry\n";
'
a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8
+9
| [reply] [Watch: Dir/Any] [d/l] |
|
Wow, thanks for the quick reply. That worrks for me.
| [reply] [Watch: Dir/Any] |
Re: Declaring an Array Using Character Classes
by frozenwithjoy (Priest) on Nov 27, 2012 at 08:47 UTC
|
Array with uppercase/lowercase alphabet and -10 to 10 using Range Operators:
$ perl -W -Mstrict -E 'my @alphanum = ( "A" .. "Z", "a" .. "z", -10 ..
+ 10 ); say "@alphanum"'
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i
+j k l m n o p q r s t u v w x y z -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1
+2 3 4 5 6 7 8 9 10
| [reply] [Watch: Dir/Any] [d/l] |
Re: Declaring an Array Using Character Classes
by rjt (Curate) on Nov 27, 2012 at 09:11 UTC
|
Hello Scott,
The replies you've already received do a great job of creating the array you asked for. I'm a little bit curious on what your intended use for this array is? I ask because I'm trying to imagine a use for this, and the only one I could come up with would be something like a password (or other random string) generator. On the chance that is what you're doing, you may want to check out CPAN modules like the following:
The list is actually quite lengthy, so I refer you to the recent research of Neil Bowers: CPAN modules for generating passwords
My apologies of course if I'm completely off the mark, but again I'm curious about your use case if you'd like to share.
| [reply] [Watch: Dir/Any] |
Re: Declaring an Array Using Character Classes
by McA (Priest) on Nov 27, 2012 at 08:48 UTC
|
my @arry = grep { is_of_some_class($_) } map { chr $_ } (1..255);
Best regards
McA | [reply] [Watch: Dir/Any] [d/l] |
|