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

qw comments

by mdupont (Scribe)
on Nov 09, 2001 at 22:33 UTC ( [id://124430]=perlmeditation: print w/replies, xml ) Need Help??

Dear fellow monks,

I have a problem with qw, one of my favorite little perl tools.

Like all our good friends, the better you know them, the more little things that get on your nerves and the more you want to change them for the better!

Other monks have meditated on qw before , here is a replacement for it! :)

I want to have it ignore everything after a # sign on a line, i have a big ol' qw and want to comment one element out. What a pain!

Also it would be nice to explicitly create an undef, newline or \r element and not have it as a string.

Maybe it would look like this :

{ my %translate =( '\n'=>"\n", '\r'=>"\r", '\t'=>"\t" ); my $regex=join "|", map { quotemeta $_ } keys %translate; sub myqw { local $_ = shift; s/\#.+//g; # bye bye comments return map { if ($_ eq '\undef') { undef; # undef element in the array } else { s/($regex)/$translate{$1}/ge; # safer than eval $_; } } split " "; } } # test it print join "|", myqw (q[ this is a set of words # and they are commented as well a b b\t\te # b tab tab e c # d # remove d because we wont need it e\ne # e newline e g \undef h ]); __END__ this|is|a|set|of|words|a|b|b e|c|e e|g||h
thanks to demerphq in advance for the review (and lots of tips) on the post!

Edit: chipmunk 2001-11-09

Replies are listed 'Best First'.
(jeffa) Re: qw comments
by jeffa (Bishop) on Nov 10, 2001 at 07:12 UTC
    If you don't mind the warnings that are generated with warnings turned on, here is a simple solution:
    my @arry = grep /^[^#]/, qw( one two three #four five );
    It doesn't catch comments from the middle, but that's because i chose not to - just a simple comment at the beginning usually suffices.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found