Preserving White Space in qw

Viewed 3239

Is there any way to preserve some white space in qw? For example:

my @a=qw(1234 John Smith 123 Main St.);

will produce an array containing 6 elements. Is there any way to, oh I don't know... escape the white space to keep some of it? Something like:

my @a=qw(1234 John\ Smith 123\ Main\ St.);

to return 3 elements: '1234', 'John Smith', '123 Main St.'?

(FYI, I've tried the above, also with varying combinations of quotes, all to no avail)

4 Answers

There multiple way to do it. My favorite is to choose a specific escape sequence to replace spaces. For example _:

my @a = qw(A_B C);
y/_/ / for @a;

or a more elaborate sequence like _SPACE_:

my @a = qw(A_SPACE_B C);
s/_SPACE_/ /g for @a;
Related