Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Escaping white space with qw//

by elbie (Curate)
on Aug 10, 2001 at 21:52 UTC ( #103970=perlquestion: print w/replies, xml ) Need Help??

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

Hi all. I have a long list of values which I want to put into an array, but some of those values have spaces in them.

It would be easiest, I feel, if I could just use qw//, but I can't figure out how to get around the splitting on whitespace.

Right now, my best solution that I can think of would be to use something in place of whitespace, and do a regex on the array later:

my @array = ( qw/one two three four_and_a_half/ ); s/_/ /g for( @array );

But this could get rather cumbersome for large arrays, and at some point in the future, I might need to use whatever character I pick within an element of the array. (Well, okay, I can pick something weird like \007, but ya never know.)

Is there another way to do this?


Replies are listed 'Best First'.
Re: Escaping white space with qw//
by dga (Hermit) on Aug 10, 2001 at 22:02 UTC

    If they're really big them maybe a __DATA__ area?

    @array=<DATA>; chop @array; #zap off \n's ... __DATA__ one two three four and a half

    If you use Inline::File then you can have multiple named DATA handles which can be accessed seperately via their <DATA> handles.

      Interesting... I seem to recall hearing the idea of multiple named __DATA__ areas before, but don't remember where.

      Too bad it doesn't work on Windows.


        I've got it working on NT, with ActiveState build 522.

        -- Frag.

Re: Escaping white space with qw//
by thatguy (Parson) on Aug 10, 2001 at 22:06 UTC
    what about assigning your values to an variable sepereated by a comma and then spliting it into an array?
    my $values="one,two,three,four and a half,five"; my @array=split(/,/, $values);
    more than one way and all that.

    Update: as dga pointed out, this would run into the same problem you are having now should a comma ever make it's way into your data.

    I think if you have a good idea of what your data will look like then use whatever you feel comfortable with as your delimiter.

      Though same problem with a character in the input data which is special.

      Though maybe if having the values in a compact area is desired.

      my $values="one , two , three , four and a half , five"; my @array=split(' , ', $values);

      This would split on a series of characters unlikely to appear naturally.

Re: Escaping white space with qw//
by scain (Curate) on Aug 10, 2001 at 22:13 UTC

    I believe both dga and thatguy are offering good examples. What you have to ask yourself is, "How do I know where the breaks should occur?" Only when you have answered that for yourself can you create a solution that will actually work. So far, you only know that white space is not the solution. So what is?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://103970]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2020-07-07 16:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found