Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Declaring an Array Using Character Classes

by simpzoid (Novice)
on Nov 27, 2012 at 08:43 UTC ( #1005810=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on Declaring an Array Using Character Classes
Replies are listed 'Best First'.
Re: Declaring an Array Using Character Classes
by kcott (Canon) on Nov 27, 2012 at 08:49 UTC

    G'day simpzoid,

    Perhaps something like this:

    $ 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

    -- Ken

      Wow, thanks for the quick reply. That worrks for me.
Re: Declaring an Array Using Character Classes
by frozenwithjoy (Curate) 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
Re: Declaring an Array Using Character Classes
by rjt (Deacon) 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.

Re: Declaring an Array Using Character Classes
by McA (Priest) on Nov 27, 2012 at 08:48 UTC

    Hi,

    probably something like this:

    my @arry = grep { is_of_some_class($_) } map { chr $_ } (1..255);

    Best regards
    McA

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005810]
Approved by 2teez
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2015-09-04 23:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred temperature scale is:










    Results (150 votes), past polls