Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Array shortcut

by Anonymous Monk
on Jul 01, 2009 at 01:12 UTC ( [id://776228]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to add a whole range of things to an array instead of typing them all out. The below has a serious syntax error. Can you help me get it to work? I also want to include the SPACE character, too.

#!/usr/bin/perl use warnings; use strict; my @chars = qq(a-z, A-Z, 0-9, !, @, #, $, %, ^, &, *); print @chars;

Replies are listed 'Best First'.
Re: Array shortcut
by GrandFather (Saint) on Jul 01, 2009 at 02:21 UTC

    Not so much a serious syntax error as a serious semantic error. You are creating a string using qq where what you really want is a list of characters. To compound the problem qq interpolates so $, (the output field separator special variable) is interpolated but generates a 'Use of uninitialized value in concatenation (.) or string at ...' warning because it is undef by default.

    You can achieve what you intend with:

    use warnings; use strict; my @chars = ('a'..'z', 'A'..'Z', '0'..'9', '!', '@', '#', '$', '%', '^ +', '&', '*'); print @chars;

    Prints:

    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*

    True laziness is hard work
Re: Array shortcut
by kennethk (Abbot) on Jul 01, 2009 at 01:24 UTC
    By using chr, you can recover a character from its index in the character set. You can therefore use that in combination with Range Operators in order to generate the kind of list you are looking for. For example:
    #!/usr/bin/perl use warnings; use strict; my @chars = map chr($_), 32 .. 122; print @chars;

    outputs

     !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz

      You can actually do some of them without needing chr. my @a = ('a' .. 'z');

      my @a = ('a'..'z', 'A'..'Z', 0..9, qw(! @ # $ % ^ & *), ' '); # maybe?

      Just another way to do it.

      UPDATE: Or you could disable that warning. It is just a warning afterall.

      -Paul

        and avoid a warning by pulling '#' out of qw:
        my @chars = ('a' .. 'z', 'A' .. 'Z', 0 .. 9, '#', ' ', qw(! @ $ % ^ & +*));
Re: Array shortcut
by YuckFoo (Abbot) on Jul 01, 2009 at 17:43 UTC
    I'd rather split single-quoted strings, easier to write (and read) than all the '',...',,'.. ,',' ,..', ', stuff. It's not like there is an endless supply of comma and quote.

    #!/usr/bin/perl use strict; my @chars = ( split('', 'abcdefghijklmnopqrstuvwxyz'), split('', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), split('', '0123456789!@#$%^&* '), ); print @chars;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://776228]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-16 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found