Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Embed empty value using qw()

by cLive ;-) (Prior)
on Sep 02, 2001 at 08:28 UTC ( [id://109700]=perlquestion: print w/replies, xml ) Need Help??

cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

More a "I wonder" rather than an "I need to know", but...

Any way to do this:

my @array = ('one', 'two', '', 'four', 'five');
using only one qw()? ie
my @array = qw(one two NULL four five);
where NULL represents the empty string?

Of course, I could do this:

my @array = qw(one two), '', qw(four five);
but that seems somehow... ugly?

And thanks to converter, I realise the difference between different releases :)

So, send me your 2 cents...

cLive ;-)

ps - well, it *is* the weekend...

Replies are listed 'Best First'.
Re: Embed empty value using qw()
by rchiav (Deacon) on Sep 02, 2001 at 09:59 UTC
    According to perlop qw() is "exactly" like doing a
    split(' ', q/STRING/);
    That doesn't leave much room for an empty value. But for another way, you could fill the array using a slice..
    @array[0..4,6..9] = qw(0 1 2 3 4 6 7 8 9);
    Still not pretty, but it's something..

    Rich

      Actually, it's more like doing that in a BEGIN block--qw(a b c) becomes ('a', 'b', 'c') at compile time (at least in newer Perls).

      =cut
      --Brent Dax
      There is no sig.

Re: Embed empty value using qw()
by trantor (Chaplain) on Sep 02, 2001 at 15:25 UTC

    What about something like:

    my @array = map { $_ eq 'NULL' ? '' : $_ } qw(one two NULL four five);

    It could be worth it when you have lots of NULLs

    -- TMTOWTDI

    Update: I didn't notice that cLive ;-) wanted an empty string and not an undef

Re: Embed empty value using qw()
by andye (Curate) on Sep 03, 2001 at 14:13 UTC
    You could do this...
    my $ns = ''; my @a = qw(one two 3 $ns five); s/(\$.*)/$1/ee foreach @a;
    andy. Update:

    Come to think of it, this seems neat & simple...

    my @a = grep {s/^''$//;1} qw(one two 3 '' four five);
    Do I get the prize? What was the prize, anyway? Hope it's ice-cream...
Re: Embed empty value using qw()
by MrNobo1024 (Hermit) on Sep 03, 2001 at 19:27 UTC
    You could use
    map { $_ ne 'NULL' && $_ } qw(one two NULL four five)
    since && returns an empty string if the first condition is false.
Re: Embed empty value using qw()
by dga (Hermit) on Sep 03, 2001 at 21:34 UTC

    Well it doesn't use qw and one would need a comment but.

    my $stuff='one, two, , three, four'; #empty element between two and th +ree my @arr=split(/, /, $stuff); #split on space comma

    Since its in single quotes it will quote sort of like qw. Optionally one could split on \s (no plus) which would mean any series of 2 spaces would be an empty value from split. And you could combine the two statements.

    my @arr=split(/\s/, 'one two three four'); #same effect as above

    Of course the empty word is really subtle in the second example.

    Update: Fixed the first RE typo as suggested.

      Er, shouldn't the regular expression for the split in the first example be /, /?

      =cut
      --Brent Dax
      There is no sig.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2024-03-29 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found